How to Fix "Warning: Colon operands must be real scalars" Warning

When I run the code below, it works as I wanted, but I get a warning. I tried different things to fix it, but of no avail. How can I refactor my code so that I will not face any related issue as the warning states if I can?
ply_nums_range = [4, 8, 16, 32, 64];
x = rand(1, 2);
data = cartesian_product({b_range, ...
N_xx_range, ...
N_yy_range, ...
N_xy_range, ...
ang_range});
for e = ply_nums_range
ply_nums = e;
for i = 1:length(data)
b = data(i, 1);
N_xx = data(i, 2);
N_yy = data(i, 3);
N_xy = data(i, 4);
ang = data(i, 5);
iteration = [a, ...
b, ...
N_xx, ...
N_yy, ...
N_xy, ...
[PLY_ANGS(ang, x), flip(PLY_ANGS(ang, x))]];
thetas = cell(1, ply_nums);
for j = 1:ply_nums
thetas{j} = sprintf("theta_%d", j);
end
variable_names = ["a", "b", "N_xx", "N_yy", "N_xy", thetas];
table_data = array2table(iteration, ...
'VariableNames', ...
variable_names);
sheet_name = sprintf("%d_ply_case", e);
writetable(table_data, ...
"design-cases/case.xlsx", ...
'WriteMode', 'Append', ...
'Sheet', sheet_name);
end
end
Console output:
Warning: Colon operands must be real scalars. This warning will become an error in a future release.
> In matlab.io.spreadsheet.internal.write.writeXLSFile>getRangeToWrite (line 575)
In matlab.io.spreadsheet.internal.write.writeXLSFile (line 262)
In writetable (line 426)
In test_case (line 72)

3 Comments

there is something odd about the way it is interpreting the sheet parameter. Please verify that sheet_name contains a valid character vector. Experiment with moving the numeric part of the character vector to the end.
I tried this like "ply_%d" for the sheet name. It does not work either.
I'm getting the same error for an all-alpha-character command. Is there any fix or workaround available?

Sign in to comment.

Answers (7)

In my case the warning was triggered because the colon operands were indexed with for-loop indices which I accidentally parsed as a column rather than a row vector.
for_indices = (1:10)'; % Column vector parsed as for-indices triggers warning.
X = 1:10;
Y = 101:110;
for i = for_indices
R = X(i):Y(1);
end
It doesn't look like this is the issue OP is facing but I thought I'd mention it in case somebody else comes across this thread.
Ryan Lamb
Ryan Lamb on 12 Jun 2024
Edited: Ryan Lamb on 12 Jun 2024
I think the Warning is coming from the 'WriteMode' being set to 'Append'.
I get the same warning any time I append to a row greater than 10.

3 Comments

I found the same thing (running 2024a). In writeLXSfile.m, on line 574, the function getRangeToWrite extracts all digits between 0 and 9 in the variable endCol into the vector startNumInRow:
startNumInRow = regexp(endCol,'[0-9]');
If endCol is 'Q102', startNumInRow is the vector [1 0 2];
That causes the warning in the next line:
endCol = str2double(endCol(startNumInRow:end)) + 1;
Replacing endCol(startNumInRow:end) with endCol(startNumInRow(1):end)
solves the problem.
"... extracts all digits between 0 and 9 in the variable endCol into the vector startNumInRow"
That regexp call is not extracting the digits, just recording their locations:
endCol = 'Q102';
startNumInRow = regexp(endCol,'[0-9]')
startNumInRow = 1×3
2 3 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.

for k = 1:numel(ply_nums_range)
ply_nums = ply_nums_range(k); %
for i = 1:length(data)
b = data(i, 1);
N_xx = data(i, 2);
N_yy = data(i, 3);
N_xy = data(i, 4);
ang = data(i, 5);
iteration = [a, ...
b, ...
N_xx, ...
N_yy, ...
N_xy, ...
[PLY_ANGS(ang, x), flip(PLY_ANGS(ang, x))]];
thetas = cell(1, ply_nums);
for j = 1:ply_nums % scalar limit
thetas{j} = sprintf("theta_%d", j);
end
variable_names = ["a", "b", "N_xx", "N_yy", "N_xy", thetas];
table_data = array2table(iteration, ...
'VariableNames', ...
variable_names);
sheet_name = sprintf("%d_ply_case", ply_nums);
writetable(table_data, ...
"design-cases/case.xlsx", ...
'WriteMode', 'Append', ...
'Sheet', sheet_name);
end
end

9 Comments

The warning suggests that vector is used as unbounded upper limit to the for loop for each iteration of outermost for loop. this can be avoided by modiyfing the code as above
ply_nums_range = [4, 8, 16, 32, 64];
for e = ply_nums_range
ply_nums = e; %inside the for loop
%....
% ...
for j = 1:ply_nums % set a scalar limit // not an unbounded upper limit
thetas{j} = sprintf("theta_%d", j);
end
No, it does not work. I still get the same warning.
Did you try the code which was given above my comment ? Or the below? Use the code given above my comment. The warning is related to : in the for loop which sets multiple upper limits during iteration
I tried both. They still do not work.
Ok. Don't try both. Use only the code given above my first comment. Otherwise Load and run the program afresh deleting the previous workspace variables. Check if you get same warning
I meant I tried both separately, and get the same warning.
Ok. May be its caused due to the filename in this line
writetable(table_data, ...
"design-cases\case.xlsx", ... % filename
'WriteMode', 'Append', ...
'Sheet', sheet_name);
my guess is that filename is relative path name. Is design-cases existent as a folder in current or remote directory ?
Though sheetname has no issues, but it may also be the cause, try to give sheetname that begins with character instead of number
sheet_name = sprintf("ply_case_%d", 4)
sheet_name = "ply_case_4"
Yes, "design-case" is an existing folder in the current folder along with the file I run so backward slash use in designating relative file path gives me an error. Also I tried the alternative sheet name scheme as you and the other user pointed out, and still get the same warning. Additionally, it is worth mentioning something I noticed in the created case.xlsx file when I look over it. I attach a related image of it below. After some column it does not write in values as expected.

Sign in to comment.

If you have a small case with which you can reproduce this behavior, could you please send it to Technical Support directly using this link? Or could you attach to this Answers post (or send it to Support) a small MAT-file with a representative table_data table array that reproduces this behavior when you run this writetable command? I'd like for us to investigate why writetable is issuing this warning message.
writetable(table_data, ...
"design-cases/case.xlsx", ...
'WriteMode', 'Append', ...
'Sheet', sheet_name);

1 Comment

I edited said .m file for minimally reproducible example of the warning, and you can find it appended to this message. I hope this helps.

Sign in to comment.

I'm getting this same error, with both writetable and writematrix. My sheet names have no digits:
writetable(toWrite,fullfile(outDir,specName),'Sheet','Spectral_Data','WriteMode','Append')
writematrix(nemList,fullfile(outDir,specName1),'Sheet','FileNames','WriteMode','Append')
This error has only appeared since updating to R2024a.
Ok, not an actual answer, but I'm certain that it's Mathworks' mistake that they have to address (before a "future release" at least.)
I tried @Ryan Lamb's suggestion and wrote 10 lines (one line at a time) using append. Adding the 10th line caused the warning:
I'll raise it as a support request as it's pretty irratating.

1 Comment

However...
I noticed that 2024b has been released. For me, this is no longer an issue.

Sign in to comment.

Hi,
I had this error message too.
I have to work loop-wise on 3 variables (assembled in an array of coloums) of case varying common lengthes.
The problem was:
-> To identify the actual length I used "size(array)" which produces a two-dimensional vector [n m], and that certainly is problematic to derive the number of loops necessary. (Un?)Fortunately Matlab 2024 was smart to anticipate what I was heading for, however producing the error meassge.
Finally I replaced "size(x)" by "length(x)" and the error message disappeared now...
BR
Meik

Products

Release

R2024a

Asked:

on 7 Apr 2024

Commented:

on 7 Nov 2024

Community Treasure Hunt

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

Start Hunting!