Error Using Plot, Invalid data argument
33 views (last 30 days)
Show older comments
when running my function i get the error messages
Error using plot
Invalid data argument.
Error in plot_chlorophyll (line 31)
plot(data.t,data.c)
my script is as follows:
function data = plot_chlorophyll(filename)
data.filename = filename;
fid = fopen(filename);
for I=1:10
tmpl=fgetl(fid);
end
J=1;
while ischar(tmpl)
tmp=textscan(tmpl,'%s %s');
data.t(J)=datenum(cell2mat(tmp{1}));
tmpc=(tmp{2});
if isempty(tmpc)
tmpc=NaN;
end
data.c(J)=tmpc;
tmpl=fgetl(fid);
J=J+1;
end
fclose(fid);
figure(1)
clf
plot(data.t,data.c,'b-')
datetick('x')
After playing with the code and searching for answers I'm still unable to plot the figure and given the error messages.
Any help would be greatly appreciated,
Thanks.
0 Comments
Answers (1)
Walter Roberson
on 3 Jan 2022
tmp=textscan(tmpl,'%s %s');
tmp will usually be a cell array with two entries, both of which are cell arrays of character vectors. However, if there was only one value on the line then tmp{2} will be an empty cell (not a cell containing an empty character vector)
data.t(J)=datenum(cell2mat(tmp{1}));
cell2mat(temp{1}} could have been written as temp{1}{1} in this particular case, but cell2mat(tmp{1}) does not hurt. Either way, the character vector is extracted and passed to datenum() with a numeric output resulting.
tmpc=(tmp{2});
if isempty(tmpc)
tmpc=NaN;
end
data.c(J)=tmpc;
The second cell is checked to see if it is empty; if it is then NaN is put in tmpc. But if it is not empty then what is stored in data.c(j) is a cell array containing a character vector. This is a case where if the first line happens to have an empty column, then data.c will be initialized as numeric and character vectors would fail after that, but if the first line happens to have a non-empty column then data.c will be initialized as a cell array and assigning NaN will be automatically converted if need be.
plot(data.t,data.c,'b-')
So at this point, is data.c a numeric array that is all NaN ? Or is a cell array that contains character vectors (at least one row) and possibly some NaN values ? Because if it is a cell array then that is not a valid parameter to pass into plot()
0 Comments
See Also
Categories
Find more on Time Series Events in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!