I have a .mat file that contains a large set of vectors. Let's say var1, var2,....varN
I need to use these vectors in a for loop. Here's what I did:
load('GivenFile.mat')
% no other variables saved
Names = who;
N = length(Names);
for i = 1:N
TempVar = evalin('base',Names{i});
...
end
I know that evalin should be avoided so I tried to save the workspace in a structure in order to do:
Tempvar = SavedWorkspace.Names{i}
But I'm not able to save the workspace as a structure.

댓글 수: 4

John D'Errico
John D'Errico 2019년 4월 2일
편집: John D'Errico 2019년 4월 2일
So, now you are learning one of the many reasons not to do as you did? I.e., don't create those reams of numbered variables? Bad programming forms just make things worse down the line.
You will be far happier if you spend the time once to combine them all into one simple array. After all, what do you think an array is, but just many rows or columns, each of which might be perceived as a vector? Simply indexed, trivially used and accessed, stored, etc. Even if the individual vectors were of varying lengths, this is why you use cell arrays.
So my recommendation is to write some code that reads it into a function workspace as a struct. Now, you will need to do the work to combine them into one array. But you need only write that code once. After all, you (or somebody) created them. Then save it back out.
Or, you can continue to work with this as it is. In that case, enjoy your ride into programming hell. The descent can be bumpy, and it gets a little warm at times.
Adriano Filippo Inno
Adriano Filippo Inno 2019년 4월 2일
John D'Errico, I just sayd that I have this .mat file... not that I created it!
I received this huge file and the only way I'm able to processing it is the upper one but I know that is should be avoided!
Rik
Rik 2019년 4월 2일
Feel free to forward this thread to the person who created this mat file. Or this thread.
Adriano Filippo Inno
Adriano Filippo Inno 2019년 4월 2일
My professor did, so is better if I don’t!

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

 채택된 답변

Rik
Rik 2019년 4월 2일

0 개 추천

You can solve it this time by loading into a struct:
S=load('GivenFile.mat');
fn=fieldnames(S);
for n=1:numel(fn)
tempVar=S.(fn{n});
%some useful code:
%...
end
But I second John's suggestion to put it all into a single variable.
As a final remark: always load into a struct, it makes it obvious where variables are coming from, which helps with debugging and future editing.

댓글 수: 2

Adriano Filippo Inno
Adriano Filippo Inno 2019년 4월 2일
Thanks Rik, this exactly solves my problem! I didn’t know that load can save to a structure! Usually I use directly a structure as coding so I never encounter this problem.
Stephen23
Stephen23 2019년 4월 2일
" I didn’t know that load can save to a structure! "
The load documentation explains all of the different load syntaxes:

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

추가 답변 (0개)

카테고리

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

태그

질문:

2019년 4월 2일

댓글:

2019년 4월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by