Hello,
I have the simple following code and it does not work of course as sprintf makes a string and cannot be entered as a value in the matrix.
Mat=zeros(64,64,round(Zlen));
for i = 1:64
for j = 1:64
for k = 1:Zlen
Mat(i,j,k)=sprintf('Ch0%d_0%d(1,%d)', i, j, k)
end
end
end
I know how Matlab work and that I cannot just hope to supress the ' by magic but I have these Data Matrix that are name as the following Ch01_01 / Ch01_02 / ... / Ch01_64 / Ch02_01 / Ch02_02 / Ch02_64 / Ch03_01 / etc..
However I still cannot find a turnaround that issue. Could you help me ?
Thank you

댓글 수: 5

Stephen23
Stephen23 2022년 10월 12일
편집: Stephen23 2022년 10월 12일
"Could you help me ?"
So far you did not tell us the most important information: how did you get all of those variables into the workspace? The place where you created those separate variables is the best place to fix your code: for example, by LOADing into an output variable.
Otherwise you will force yourself into writing slow, complex, inefficient, obfuscated, insecure, buggy code which is hard to debug: http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Julien Maxime
Julien Maxime 2022년 10월 12일
That is the issue. It is data exported from experimental software. I cannot change the name of the files before they are imported in the Workapace as such when i open the .mat file
Stephen23
Stephen23 2022년 10월 12일
편집: Stephen23 2022년 10월 13일
"I cannot change the name of the files ..."
The filenames are irrelevant to what variables are in the MATLAB workspace.
"...before they are imported in the Workapace as such when i open the .mat file"
Then there is nothing stopping you from calling LOAD with an output argument:
S = load(..)
Julien Maxime
Julien Maxime 2022년 10월 13일
What is stopping me is that when i open the .mat matrix, i have more than 4000 files opening in the workspace and to open them all with load command I would need a loop and call them one by one, coming back to my initial issue.
Stephen23
Stephen23 2022년 10월 15일
"What is stopping me is that when i open the .mat matrix, i have more than 4000 files opening in the workspace and to open them all with load command I would need a loop and call them one by one, coming back to my initial issue."
So far everything you describe sounds like it could be avoided by LOADing into an output variable, as already shown.

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

 채택된 답변

Jan
Jan 2022년 10월 15일

0 개 추천

Stephen an Walter hit the point.
% Store imported data in an array instead of polluting the workspace:
FileData = load('YourInputFile.mat');
% No loop over k required:
Mat = zeros(64, 64, round(Zlen));
for i = 1:64
for j = 1:64
Mat(i, j, :) = FileData.(sprintf('Ch0%d_0%d', i, j));
end
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 10월 12일

1 개 추천

Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for information about why we strongly recommend against creating variable names dynamically.

카테고리

질문:

2022년 10월 12일

답변:

Jan
2022년 10월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by