For loup not working

Hey everyone!
I am using this code with a for loop to set paramaters of a FMU.
It worked fine until I added a new parameter into VSDConfig.xlsx table.
The function still works for most parameters. However, the new parameter is set to 0 instead of the actual value.
Also, this only happens when the for loop is used. When I tried to set the new paramater by indexing its number, it worked fine. see below:
SetFmuParam(fmuImportName,string(VarNames(19)),cell2mat(Vars(19))); (worked)
I have run a loop to check if the indexing is right and it was:
for i=1:length(VarNames)
string(VarNames(i))
cell2mat(Vars(i))
end
result for i=19:
...
ans =
"Industry.M5_Application.M5_3_Load.M5_3_2_Inertia.P5_3_2_3_System_Inertia"
ans =
0.0104
...
Everything seems to work fine part by part, until I put it together using the for loop.
Please help.
Original code:
function FMUConfig(PumpDisp)
fmuImportName = 'iC7_IndustryDriveTrain_Uut/FMU';
fmuName = get_param(fmuImportName,'FMUName'); % Finds FMU name
filename = "VSDConfig.xlsx";
VSDConfig = readcell(filename);
missing = cellfun(@anymissing, VSDConfig);
VSDConfig(missing) = {0};
data = VSDConfig(2:end,2:end);
RowNames = VSDConfig(2:end,1);
VarNames = VSDConfig(1,2:end);
idx = [RowNames{:}] == PumpDisp;
Vars = data(idx,:);
for i = 1 : length(VarNames)
SetFmuParam(fmuImportName,string(VarNames(i)),cell2mat(Vars(i)));
end
end

10 Commenti

Image Analyst
Image Analyst il 29 Gen 2026 alle 16:16
It's hard to tell without stepping through it with your data and debugging it. Did you do that?
Can you work with the variables in their original format rather than converting them to a string or character array?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
which -all SetFmuParam
'SetFmuParam' not found.
doesn't appear to be a Mathworks=supplied function so we can't see what might be going on there.
As @Image Analyst notes, reading the data directly as its variable types would almost certainly simplify the coding and debugging significantly.
tData=readtable(filename,'ReadRowNames',1,'ReadVariableNames',1);
looks like would work well...
Simon
Simon il 30 Gen 2026 alle 7:31
@Image Analyst Yes, I have tried to debug the code. As I have mentioned, I have run the parts of the code and to check where might be the problem.
SetFmuParam works without any issues
for loop lists all the parameters without issue
But when I try to use the forloop together with the SetFmuParam it doesn't work properly with the parameter that was added recently.
I have to convert the data to char and string so that the SetFmuParam can read them.
Unfortunatelly, I cannot attach all the data, because it is a combination of several matlab scripts, excel table and a simulink model with FMU unit I have mentioned. it's a lot of data and the FMU unit is not for sharing.
Simon
Simon il 30 Gen 2026 alle 7:42
You're right, it's not a Mathworks function. This function came with the FMU unit and is used to change parameters of the FMU in simulink via a script.
>> tData=readtable("VSDConfig.xlsx",'ReadRowNames',1,'ReadVariableNames',1);
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names
for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
The thing is that "VSDConfig.xlsx" contains data with headers that are quite long and the length cannot be preserved in table format. These names need to be used to find the exact parameter in the FMU unit that should be changed.
But again, there is no issue in any of the given functions when run part by part.
The problem is within the for loop and I don't see any reason for that.
Image Analyst
Image Analyst il 30 Gen 2026 alle 13:59
When does it fail with the for loop? If you say
for i = 1 : 1
SetFmuParam(fmuImportName,string(VarNames(i)),cell2mat(Vars(i)));
end
does it run? If you then change the final i to 2 does it then crash on the second iteration? What iteration does it crash on?
One thing we recommend is don't use i and j as loop iterators because they are the imaginary variable sqrt(-1). Is it possible some variable is imaginary and your code might be confusing your i loop iterator with the imaginary variable? Try replacing i with k and see if it works.
dpb
dpb il 30 Gen 2026 alle 16:28
Forgot to add it, sorry. Use
tData=readtable("VSDConfig.xlsx",'ReadRowNames',1,'ReadVariableNames',1,'VariableNamingRule','preserve');
Then you can address the variable names via
vnames=tData.Properties.VariableNames;
identially as they are provided for string matching.
You still haven't shown nor told us what the specific error is or where. Copy and paste the calling code and then all the error message text.
At least attach the spreadsheet file and show the prototype of the called function that could be used to mimic the whole thing.
Simon
Simon il 2 Feb 2026 alle 7:46
@Image Analyst It doesn't fail per say. The loop runs through all the iterations. However, it set 19th iteration to wrong value. I thought there might be a problem with this value because I added it as the last one. The function was woring without any flaws until I added this parameter. I don't know if this has something to do with it but this is how the 19th parameter differs from the others.
@dpb As I have told you. I cannot use it as a VariableName, Because the number of allowed charactersfor VariableName is limited to 63 characters. For instance, this is the name of the 19th parameter that causes issues:
Industry.M5_Application.M5_3_Load.M5_3_2_Inertia.P5_3_2_3_System_Inertia
If I tried to later use it for the SetFmuParam function, it won't work.
When it comes to error. There is no error. The function runs throught all the iterations, but after the function runs the 19th parameter is always set to 0(zero). No matter the number in "VSDConfig.xlsx" source data.
You can find the "VSDConfig.xlsx" in the attachments.
Simon
Simon il 2 Feb 2026 alle 8:34
Hey guys, I am very thankful for your willingness to help. I have found the cause of the problem. I have to say it was really a stupid one. I am sorry I brought you into this for no reason.
The problem was in "VSDConfig.xlsx" there were two columns with the same name: Industry.M5_Application.M5_3_Load.M5_3_2_Inertia.P5_3_2_3_System_Inertia
There were the correct values in the first column, while the second was empty. There is a section of the code that changes missing values to 0.:
missing = cellfun(@anymissing, VSDConfig);
VSDConfig(missing) = {0};
Now with the second empty column being after the first column with the correct values, the for loop used the second one setting the parameter value to 0.
Thanks again for your willingness and sorry for this stupid issue on my end.
dpb
dpb il 2 Feb 2026 alle 14:05
Thanks for letting know that the issue was uncovered.
Just for the future, note that the problem could have been found much sooner if had attached the input file with the initial question or at least as follow-up when requested. It oftentimes as here is simply not possible to diagnose a problem without having the necessary input to be able to reproduce the issue. So, when it's time to ask again, "help us help you!" by making it possible to reproduce the problem easily.
Simon
Simon il 3 Feb 2026 alle 6:02
Sure, sorry, I will remember that. Thanks again.

Accedi per commentare.

Risposte (1)

dpb
dpb il 30 Gen 2026 alle 18:43
"...However, the new parameter is set to 0 instead of the actual value."
function FMUConfig(PumpDisp)
fmuImportName = 'iC7_IndustryDriveTrain_Uut/FMU';
fmuName = get_param(fmuImportName,'FMUName'); % Finds FMU name
filename = "VSDConfig.xlsx";
VSDConfig = readcell(filename);
missing = cellfun(@anymissing, VSDConfig);
VSDConfig(missing) = {0};
...
The symptoms are entirely consistent with the above having set that column of data to zero...your test loop doesn't test the values you actually passed, only something read. But, we can't debug without the data to be able to reproduce what may have caused/is causing the problem.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Prodotti

Release

R2024b

Richiesto:

il 29 Gen 2026 alle 13:44

Commentato:

il 3 Feb 2026 alle 6:02

Community Treasure Hunt

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

Start Hunting!

Translated by