Plot Using Variable Name From For Loop
显示 更早的评论
I have a number of variables (for example timeoffset0 - timeoffset20 and receivedpoweroffset0 - receivedpoweroffset20). I would like to plot all of these variables within a loop or similar method so that I can write a script which will plot all of the variables for any number of variables that are given as an input.
I have tried to do this using a for loop in the two following methods:
for fractionoffset=0:1:20
plot(['timeoffset',fractionoffset],['receivedpoweroffset',fractionoffset])
end
or
for fractionoffset=0:1:20
plot(sprintf('tdownsampoffset%d', fractionoffset),[sprintf('r1dbdownsampoffset%d', fractionoffset)
end
Neither of these work since MATLAB is trying to plot the text rather than the variable referenced by the text.
The error is: Error using plot Invalid first data argument.
How would I go about doing this? The different numbered variables have different numbers of elements so they cannot be easily combined into one variable.
EDIT: moved "Answer" here:
Here attached are two pairs of files which I am struggling to read into one variable (i.e. one variable for td and one variable for db) (one column for 2 and one column for 3 within each variable). If this can be done without having to add a zero at the beginning or end of the variables ending in three (which contain one less element) then I can easily plot them from a loop.
4 个评论
jonas
2018-7-24
May I avsked where those variables come from? It'd be easier to avoid this situation.
Benjamin Bloomfield
2018-7-24
jonas
2018-7-24
Surely! Can you provide 2-3 tricky txt-files to work with?
"The different numbered variables have different numbers of elements so they cannot be easily combined into one variable."
Then use a cell array. With a cell array you can trivially use efficient indexing, which is much simpler than buggy, inefficient dynamic variable names:
Using dynamic fieldnames is a significant improvement on dynamic varaible names, but requires more complexity than you need: indexing is really simple, so why not use it?
采纳的回答
更多回答(1 个)
Here is one way to store the data in a single struct using dynamic field names. The data is stored in a single folder, without any other txt-files.
data=struct;
files=dir('filepath\*.txt');
%%Preallocate array with fieldnames
fieldnames = strings(numel(files),1)
%%Create vector with fieldnames and save data
for i=1:numel(files)
str=strsplit(files(i).name,'.');
fieldnames{i}=str{1}
data.(fieldnames{i})=dlmread(files(i).name);
end
Now you can easily call a subset of data, e.g.:
plot(data.td2,data.db2)
or something like this
plot(data.(fieldnames{3}),data.(fieldnames{1}))
or if you want to use a loop to plot pairs
figure; hold on
for i=2:3
plot(data.(['td',num2str(i)]),data.(['db',num2str(i)]))
end
which gives the attached figure.
In conclusion, dynamic field names are much more convenient to work with than dynamic variable names :)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!