Printing figures without their annotations now?

For like no reason, matlab is printing figures with everything but an annotation I want on them. I have never had this problem and don't know what's happening.
Code is set up in a for loop for 6 iterations. I pre defined a 1x6 handle array, I then loop through each one and make a plot with axis scaling, limits etc. I then added an annotation for each one, and when I created a for loop to print them all to .png, for some reason the annotations just aren't included (even if I strip back all the formatting to defaults).
I've used this method several times and I have never experienced this problem so I really have absolutely no idea what's causing it.
Cheers

7 commentaires

Rik
Rik le 13 Mai 2020
Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue.
Hugh Wheaton
Hugh Wheaton le 13 Mai 2020
Modifié(e) : Hugh Wheaton le 13 Mai 2020
I literally can't recreate it without the exact code I am using so I am posting everything:
n=6;
fig3=handle(zeros(1,n));
P1=struct;
f=struct;
for i=1:n
% Copied from fft_CP1_v2.m
[data,fs]=audioread(sprintf('%sfiles\\Trumpet %d [2020-05-13 171858].wav',dir,i));
L = length(data);
fprintf('Number of samples in "Trumpet %d [2020-05-13 171858].wav" file: %d\n',i,L);
fprintf('Sampling rate of "Trumpet %d [2020-05-13 171858].wav" file: %f Hz (samples per second)\n',i,fs);
data_fft = fft(data);
P2 = abs(data_fft/L);
P1.(alph(i)) = P2(1:fix(L/2)+1);
P1.(alph(i))(2:end-1) = 2*P1.(alph(i))(2:end-1);
% define the frequency domain:
f.(alph(i)) = fs*(0:(L/2))/L;
% actually make the plot:
fig3(i)=figure;
annotation(fig3(i),'textbox',[0.4 0.8 0.45 0.1],"String",sprintf("Samples in Trumpet %d: %d\nSampling rate of Trumpet %d: %dHz",i,L,i,fs),"BackgroundColor","#FFCC99","EdgeColor","#FFCC99","Color","#003366","FontName","BahnSchrift","FaceAlpha",0.4,"Visible","on","FitBoxToText","on")
bar(f.(alph(i)),P1.(alph(i)));
title(sprintf("Figure 3%c - Line Spectrum for Trumpet %d.wav",alph(i),i))
xlabel('f (Hz)');
ylabel('|P1(f)|');
xlim([0 6000])
ylim([0 0.05])
print(gcf,sprintf("%s%s\\Trumpet%d",dir,folder,i),'-dpng','-r600');
end
It isn't the handle array, it isn't the bar chart and it isn't to do with the formatting of the annotation either because I've literally perfectly recreated this whole thing with a 2 iterator figure plotting the same, but simplified plots, so I assume it is something to do with how the handles are iterated at a higher level I am not aware of...
I am out of ideas as to what it is now, I've tried rearranging the code a heap, I have tried changing it from a fixed handle reference to gcf in the loop, iterating over them after, adding the annotation last, adding it before, plotting nothing doesn't print the annotation either. I'm really perplexed as to wtf it is causing this...
Edit: fyi dir and folder were previously defined strings that concatenate directly into the desired path if that's not obvious.
Oh and alph is literally just 'abcdefghijklmnopqrstuvwxyz' defined earlier as well. I just did this to bypass the not being able to iterate through a struct problem (as the array sizes will not be consistent).
Rik
Rik le 13 Mai 2020
I still can't run this code, because source files are missing. Putting variables in a mat file is also fine, but we need to be able to run your code as posted and see the same thing you see. If we can't run your code we have to read your code, which is a difficult way to find errors.
is it okay just to upload some wav files? I'd have thought any wav files would do it but I'll upload them here. I struggle to see how the files would have anything to do with it as they print out fine, just without the annotation....
If this isn't good enough, I'll write up some code tomorrow as it is getting quite late.
OneDrive link ^
I'd imagine this is quite close to being able to run the code as posted as you can just dump everything into some temp folder somewhere, define alph by copying my above post and then defining dir as this temp folder directory and folder as '', right?
Do you have any potential ideas as to what is going on here? Have you ever seen something like this before? Like I said, I'm having a real hard time trying to re-create this problem deliberately...
Rik
Rik le 13 Mai 2020
I can't reproduce your issue, see attached screenshot of Matlab figure and output image.
Have you modified any settings to do with the renderer?
(off topic: I would suggest not shadowing the dir function with a variable, and to use fullfile to create a file name from the components)
Hugh Wheaton
Hugh Wheaton le 14 Mai 2020
Modifié(e) : Hugh Wheaton le 14 Mai 2020
no I don't think so. I changed whether or not matlab includes NaN in calculations once, but I switched that back shortly after. In fact, I've recently done a completely fresh install of 2019b after failing to clone my HDD to a new SSD, and yet the problem is still occurring.
Thanks for the fullfile advice, I've just switched that up, and I forgot dir was a function so I fixed that too. Unfortunately the annotations still aren't copying and if you can't recreate the issue I'm getting really frustrated because these kinds of things seem to happen every time I attempt something at matlab. It's not too important, but my OCD isn't letting this go...
What settings should I check out with regards to the renderer? I'm on a laptop running hybrid graphics processing, so any chance this could maybe have something to do with it?
Edit:
Could it be running it from an mlx file that's doing it? I've run into quite a few bugs using that feature...
Edit 2:
yep, I literally copied the entire thing into a .m file and it was fine. I assume it is just another mlx bug.
Can I just suggest that mathworld focus on their mlx feature because I keep running into bugs (one big one is that clicking "hide code" on the view layout options corrupts your file irreversibly, you have to manually copy all lines of code into a blank file using the compare function).
Rik
Rik le 14 Mai 2020
I tend to not use live scripts, so I didn't even think to check. I know Mathworks is putting a lot of time in the further development of live scripts, but I'm not an employee. So if you have suggestions and/or bug reports you should contact them directly.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2019b

Commenté :

Rik
le 14 Mai 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by