using a function in a for loop

3 次查看(过去 30 天)
I have a function that interrogates a file and returns a matrix and a boolean. I would like to run this for 12 different parts of a file. The below gives an error that brace indexing is not supported for this variable type. Is there a way to write the below?
for k = 1 : 12
[CH{k} result{k}] = ytmWdfGetData(fileName, k, 1)
end

采纳的回答

Image Analyst
Image Analyst 2022-11-14
Your simplified code works fine. Watch:
fileName = 'snafu.txt';
for k = 1 : 12
[CH{k} result{k}] = ytmWdfGetData(fileName, k, 1);
end
celldisp(CH)
CH{1} = 0.2304 CH{2} = 0.8215 CH{3} = 0.3306 CH{4} = 0.0877 CH{5} = 0.6618 CH{6} = 0.8029 CH{7} = 0.3256 CH{8} = 0.5995 CH{9} = 0.5077 CH{10} = 0.1808 CH{11} = 0.6645 CH{12} = 0.1297
celldisp(result)
result{1} = 0.3809 result{2} = 0.8788 result{3} = 0.1505 result{4} = 0.9295 result{5} = 0.3460 result{6} = 0.2646 result{7} = 0.2070 result{8} = 0.8174 result{9} = 0.4201 result{10} = 0.2801 result{11} = 0.1869 result{12} = 0.2093
fprintf('Done!\n')
Done!
function [out1, out2] = ytmWdfGetData(arg1, arg2, arg3)
out1 = rand;
out2 = rand;
end
Your error comes from some other part of your code.
Please attach the complete error message -- ALL THE RED TEXT. This includes the line number, the actual line of code that threw the error, and the error description itself.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
  2 个评论
Ted HARDWICKE
Ted HARDWICKE 2022-11-14
编辑:Ted HARDWICKE 2022-11-14
Thanks @Image Analyst. I will add the error message in full next time.
Error message was
Unable to perform assignment because brace indexing is not supported for variables of this type
There are no other parts of my code other than defining the fileName, which works in other functions in this toolbox (wdf access). (I was using 'evaluate selection in command window')
What happened, I assume, is I had CH in workspace from a previous attempt and its type did not support braces. I deleted that from workspace and reran, and it worked as you show above.
Is there a way to get multiple matrices - CH1 matrix, CH2 matrix, etc in the for loop that reads the file/uses the wdf funtion? as opposed to CH() 1x12 cell array with each cell being a 1x10M array? I can work with what I have, and might want to, but will have to modify other parts of my script.
Image Analyst
Image Analyst 2022-11-14
@Ted HARDWICKE no. Well yes, but they're kludgy and not recommended. For example one way is
for k = 1 : 3
if k == 1
[CH1, result1] = ytmWdfGetData(fileName, k, 1);
elseif k == 2
[CH2, result2] = ytmWdfGetData(fileName, k, 1);
elseif k == 3
[CH3, result3] = ytmWdfGetData(fileName, k, 1);
end
end

请先登录,再进行评论。

更多回答(1 个)

Vilém Frynta
Vilém Frynta 2022-11-14
编辑:Vilém Frynta 2022-11-14
I would recommend define your variables beforehand with zeros(). This way, you may avoid your brace indexing problem. Then you can asign function results to your variables directly from function (v1) or indirectly (v2).
I'll try to show an example:
v1
% Create zeros beforehand, you will asign function results here
CH = zeros(1:12)
results = zeros(1:12)
for k = 1 : 12
[CH(k) result(k)] = ytmWdfGetData(fileName, k, 1)
end
v2 - little diferent version, that I would say is "safer"
% Create zeros beforehand, you will asign function results here
CH = zeros(1:12)
results = zeros(1:12)
for k = 1 : 12
[X Y] = ytmWdfGetData(fileName, k, 1) % Save function results to X and Y
CH(k) = X; % Save X and Y to CH and results
results(k) = Y;
end
If you want different variable type (cell, structure), you can change it as you desire; you can use something else than zeros, jsut define your variables and use indexing.
EDIT: Added second version.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by