Putting similar named variables into one vector or structure

Hi,
I'm trying with no succes to put variables that have the same construct into one vector: C_100, C_200 and so on into vedctor C. I also want to put Arrays of (44,2) with similar names into a structure: Q_100, Q_200 and so on into a structure Q. Is there a simple way to loop through it?
Thanks in advance, Asher

댓글 수: 6

Stephen23
Stephen23 2015년 7월 21일
편집: Stephen23 2015년 7월 21일
Instead of creating all of these separate variables and then hacking them together, it would be much more reliable , faster and tidier to actually generate them in one variable to begin with (e.g. in a multidimensional numeric array, or a cell array). Read the answers to know why.
Thanks Stephan. The Data is coming from different runs (on optimization model). each run is seperate and creates a .mat file. I don't see how I can bunch them together before hand.
You don't have to merge the data beforehand.
Presumably you are load-ing the .mat files in a loop: in this case, instead of loading the .mat file directly into your workspace, simply load each file using the output argument:
S = load(...);
This output can be allocated to a cell array, or whatever data structure is most appropriate for your data. Probably the best would be to use a non-scalar structure, which sounds about perfect for your data (pseudocode):
for k = 1:numfiles
S(k) = load(filename(k));
end
Then you have just one variable with all of your data in it: This will be much tidier and more robust than hacking dynamic variable names! It also has lots of useful ways to access the data: see the links I gave.
Thanks again. I see what you mean and I have a lot to learn. for some reason (subscript assignment between dissimiliar structures error) I cna't save the structure in a structure but it does work saving it into a cell array. is there a simple explantion?
It sounds like your .mat files have different data in them, and so the output of load has different fields in its structure. Structures with different fields cannot be concatenated together, thus the error. One possible solution is to use load's optional argument to only read the same variable/s from each .mat file. Here is a complete working example of how to do this:
A = [1,2,3];
save('temp1','A');
A = [4,5,6];
save('temp2','A');
A = [7,8,9];
B = NaN;
save('temp3','A','B'); % <- different number of fields!
D = dir('temp*.mat');
N = {D.name};
for k = numel(N):-1:1
S(k) = load(N{k},'A'); % Specify 'A' only
end
If you remove the 'A' from the load command then it give the same error as you are getting, but with this optional argument it will only load the A field, so no error. Here are some ways of accessing the data in the structure S:
>> S(2).A % indexing single element
ans =
4 5 6
>> horzcat(S(2:3).A) % subset indexing
ans =
4 5 6 7 8 9
>> vertcat(S.A) % all data
ans =
1 2 3
4 5 6
7 8 9
>> {S.A} % all into a cell array
ans =
[1x3 double] [1x3 double] [1x3 double]
And believe you me, this is much more reliable and faster than trying to use dynamically named variables!
Thanks!! again. Have been working with the structs. Very neat and comfortable. Here is a question that arose: I have 7 data files with the same variables. but on the seventh one I forgot to create one of them. I put them all in a struct: S(1), S(2) and so on. in a loop but the seventh one won't go in understandably because it doesn't have the same fields. So I added the variable (h) to the seventh mat file but for some reason it still won't be joined to the struct. I am attaching 7 of the files. the one named 7 won't be added to the other 2. I would love to understand the reason. this is the loop I wrote:
for i=1:7
aa=sprintf('NYT_outout_%d_years.mat',i);
S(i)=load(aa);
end
Thanks a lot in advance. Asher

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 21일
편집: Azzi Abdelmalek 2015년 7월 21일
v={'C_100','aze','C_200'}
C=regexp (v,'C_\d+','match')
idx=~cellfun (@isempty,C)
outc=C (idx)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2015년 7월 21일

편집:

2019년 6월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by