My plot is empty...?

28 vues (au cours des 30 derniers jours)
Agustina Magarinos-Rodriguez
Hello. I am trying to create a plot containing data from multiple files. I have producecd a code that seems to be running (no errors come up when trying to run it). However, the plot is blank. The code will create a figure window and a plot with axes, titles and everything, but it does not display my data. I have left my code below, does someone spot any mistake or where I could have gone wrong?
%Find and sort files from directory
P = 'C:/Users.../' ; %Working directory (my directory, it is the correct one I have triple checked)
S = dir (fullfile (P , 'PowerMat_*.mat')) ; %All of the files have the same structure for the name. They are PowerMat_xx_xx_xx... .mat (xx are numbers, they are a pretty big list of different parameters that vary with each simulationseparated by _)... not sure this is the correct syntax to recall them?
S = natsortfiles (S) ; %Sort folders / file names into natural order
%Set conditions for plot
x = 15 : 1 : 50 ; %Array of z values on zmin_15 (from 15 to 50 in steps of 1) for x axis
n = 39 ; %Nº of column to read of the PowerMat (each column is a w value)
%Start plot
figure(1) ;
hold on ;
for k = 1 : numel (S)
fn = S(k).name ; % filename : used for legend
fn = strrep (fn , ' ' , ' ') ;% replace _ by space for better legend rendering
leg{k} = fn ; % store in cell array
% load data
F = fullfile (S(k).folder , S(k).name) ;
data = load (F) ; % is structure
names = fieldnames (data) ; % get fieldnames
y = data.(names{1}) ; % your data (assumed in first fieldname)
y = y (: , n) ; %Selecting column n on data
plot (x , y)
end
set (gca , 'YScale' , 'log') %Sets y axis to be logarithmic scale. Comment for linear scale
title ('title') ;
xlabel ('x label') ;
ylabel ('y label') ;
hold off
  2 commentaires
Johan
Johan le 7 Juil 2022
You are plotting in log scale, have you checked that your data is positive ? It looks like Matlab plot nothing in log scale if you have less than 2 consecutive points and you are ploting lines.
x = linspace(1,10);
y = x-x(end-1);
y(5) = 10;
plot(x,y)
set(gca,'YScale','log')
plot(x,y,'*')
Warning: Negative data ignored
set(gca,'YScale','log')
Warning: Negative data ignored
Agustina Magarinos-Rodriguez
All of my data is positive. What I have is a folder with a bunch of files. From each file the code should be reading all rows of data from a specific column and I should have 36 consecutive datapoints being read in each file. Whatever those points are they should be plotted as a line in log scale for the sake of accessibility and making the plots easier to analyse. I have tried leaving it in a linear scale but the data won't show either.

Connectez-vous pour commenter.

Réponses (3)

Image Analyst
Image Analyst le 7 Juil 2022
This should really be a FAQ since it's asked so often. You're probably plotting a single data point and you didn't call "hold on" so the next time it goes to call plot, it blows away the previously plotted point.
To fix, either put hold on
plot(x, y, 'b.', 'MarkerSize', 20);
grid on;
hold on;
or call plot after the loop and pass it the whole vectors.
If you have any more questions, then attach your data (mat files) and code to read it in with the paperclip icon after you read this:
  2 commentaires
Agustina Magarinos-Rodriguez
I do have the hold on and hold off command in the code starting before the for loop and ending at the very end of the code. It seems to not recognise the y variable when running it now?
Image Analyst
Image Analyst le 7 Juil 2022
Did you overlook the part where I said:
If you have any more questions, then attach your data (mat files) and code to read it in with the paperclip icon after you read this:

Connectez-vous pour commenter.


Torsten
Torsten le 7 Juil 2022
Change
x = 15 : 1 : 50 ;
to
x = (15 : 1 : 50).' ;
  5 commentaires
Image Analyst
Image Analyst le 7 Juil 2022
You still haven't attached 'PowerMat_*.mat'. We'll check back later for it.
Agustina Magarinos-Rodriguez
All the files have been added now. Sorry and thank you

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 8 Juil 2022

Community Treasure Hunt

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

Start Hunting!

Translated by