From: Florent on
Hi,
I used int2str in a loop to load several files containing data. I want to define each time a matrix out of this, but when defining it, it gives me a string.


for i=1:8
s=['load S_d' int2str(i) '.txt']
eval(s)
F=[' S_d' int2str(i) '']
eval(F)
%it gives me a string and I would like F to be a matrix S_d1, then S_d2, etc.

% I want also to save that after calculations, is the following correct? How do I save in .txt?
t=['save B' int2str(j) '.mat']


Can someone help me?
Thanks in advance!
Florent
From: dpb on
Florent wrote:
> Hi,
> I used int2str in a loop to load several files containing data. I want
> to define each time a matrix out of this, but when defining it, it gives
> me a string.
>
>
> for i=1:8
> s=['load S_d' int2str(i) '.txt']
> eval(s)
....
Yeah, don't do that... :) (See FAQ at
<http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_process_a_sequence_of_files.3F>

and the related on 'spoofing' variables and evils of eval...)

--

From: Jan Simon on
Dear Florent,

EVAL is a very "dangerous" function, which cause programs to be hard to debug and instable. It is possible to avoid EVAL, ASSIGNIN and EVALIN by using other approachs.

for i = 1:8
s = load([S_d' int2str(i) '.txt']);
f = s.([' S_d' int2str(i)]); % See "dynamic field names"
...
end
B = rand(10, 3); % Test data
save('B', [sprintf('FileName%d.mat', j]);

Good luck and welcome to Matlab, Jan
From: Jan Simon on
Dear Florent,

Instead of personal mails containing "It didn't work... ", it is in general more successful to post the code you've tried together with the error message in the news group.

Thanks and kind regards, Jan
From: Florent on
Hi,
Jan gave me a solution, but it failed, with this error message:
??? Undefined function or variable
'S_d'.

Error in ==> essai_int2str_jan at 3
s = load([S_d' int2str(i)
'.txt']);

I tried also with the former coammand: s=['load S_d' int2str(i) '.txt']
and it gave me another message:
??? Attempt to reference field of
non-structure array.

Error in ==> essai_int2str_jan_a at
6
f = s.([' S_d' int2str(i)]); %
See "dynamic field names"

I went on the help, but didn't find the solution.
Florent



"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i2f60v$jvu$1(a)fred.mathworks.com>...
> Dear Florent,
>
> EVAL is a very "dangerous" function, which cause programs to be hard to debug and instable. It is possible to avoid EVAL, ASSIGNIN and EVALIN by using other approachs.
>
> for i = 1:8
> s = load([S_d' int2str(i) '.txt']);
> f = s.([' S_d' int2str(i)]); % See "dynamic field names"
> ...
> end
> B = rand(10, 3); % Test data
> save('B', [sprintf('FileName%d.mat', j]);
>
> Good luck and welcome to Matlab, Jan
 |  Next  |  Last
Pages: 1 2
Prev: about reading the images
Next: NI DAQ for Simulink