Filter löschen
Filter löschen

Plot Using Variable Name From For Loop

24 Ansichten (letzte 30 Tage)
Benjamin Bloomfield
Benjamin Bloomfield am 24 Jul. 2018
Bearbeitet: Stephen23 am 24 Jul. 2018
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 Kommentare
jonas
jonas am 24 Jul. 2018
Surely! Can you provide 2-3 tricky txt-files to work with?
Stephen23
Stephen23 am 24 Jul. 2018
Bearbeitet: Stephen23 am 24 Jul. 2018
"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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 24 Jul. 2018
Bearbeitet: Stephen23 am 24 Jul. 2018
Here is an outline of one simple way to do this:
N = 20;
db = cell(1,N);
td = cell(1,N);
for k = 0:N
db{k} = dlmread(sprintf('db%d.txt',k));
td{k} = dlmread(sprintf('td%d.txt',k));
end
and then plotting in a loop is trivial too, using simple, very efficient indexing:
for k = 1:N
plot(db{k},td{k})
hold on
end
Much better than magically accessing the variable names!

Weitere Antworten (1)

jonas
jonas am 24 Jul. 2018
Bearbeitet: jonas am 24 Jul. 2018
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 :)

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by