First cell in a string array is empty, how to replace this?

1 view (last 30 days)
Hello everyone,
The code below shows a while loop. I am assigning a Pass or Fail string based on a certain condition. The code works fine but the string array is 1x12 instead of being 1x11. The first cell of this array is also empty and the second cell till the 12th contains the fail or pass string. How can I make sure that the first cell is not empty and has the fail or pass string?
Thank you.
n = 1;
sigma_f = [118.01 97.84 84.51 67.98 58.13 50.29 45.08 41.36 38.58 36.42 34.70];
m_sigma_yf = 280;
s_factor = 5;
mode1_status = strings(size(11));
while n < 12
if sigma_f(n) < m_sigma_yf/s_factor
mode1_status(end+1) = 'Pass';
else
mode1_status(end+1) = 'Fail';
end
n = n+1;
end
  1 Comment
Umar
Umar on 10 Jul 2024

Hi Dwayn,

Based on the code you provided, it seems like the issue with the first cell being empty in your string array `mode1_status` is due to the initialization of the array with `strings(size(11))`. This creates an array of empty strings with a size of 11, which results in the first cell being empty when you append values to it within the while loop.

To ensure that the first cell is not empty and contains the fail or pass string, you can modify the initialization of `mode1_status` to include an initial value. You can set the first cell of the array to either 'Pass' or 'Fail' before entering the while loop. Here's an updated version of your code:

n = 1;

sigma_f = [118.01 97.84 84.51 67.98 58.13 50.29 45.08 41.36 38.58 36.42 34.70];

m_sigma_yf = 280;

s_factor = 5;

% Initialize as a 1x12 string array

mode1_status = strings(1, 12);

% Set initial value for the first cell

if sigma_f(1) < m_sigma_yf/s_factor mode1_status(1) = 'Pass'; else mode1_status(1) = 'Fail'; end

while n < 12 if sigma_f(n) < m_sigma_yf/s_factor

% Adjust index to avoid overwriting initial value

        mode1_status(n+1) = 'Pass';
    else

% Adjust index to avoid overwriting initial value

        mode1_status(n+1) = 'Fail';
    end
    n = n + 1;
end

By setting an initial value for the first cell of `mode1_status` before entering the while loop and adjusting the index inside the loop, you can ensure that the first cell is not empty and contains the fail or pass string based on your condition. Feel free to test this updated code and let me know if you encounter any further issues or require additional assistance.

Sign in to comment.

Accepted Answer

Aquatris
Aquatris on 10 Jul 2024
Edited: Aquatris on 10 Jul 2024
The issue is you are not initiating and filling the mode1_status matrix properly. Your initial mode1_status matrix is a 1x1 matrix, instead of 1x11 as you wanted. So in your while loop you assign something to (end+1) index, where for the first time, it filles index 2. Not the thing you want.
A simple fix is by replacing your while loop with for loop to prevent (end+1) type indexing. There is no point using while loop for this which creates your issue.
n = 1;
sigma_f = [118.01 97.84 84.51 67.98 58.13 50.29 45.08 41.36 38.58 36.42 34.70];
m_sigma_yf = 280;
s_factor = 5;
%mode1_status = strings(size(11)); % THIS IS WRONG
mode1_status = strings(1,11);
for n = 1:11
if sigma_f(n) < m_sigma_yf/s_factor
mode1_status(n) = 'Pass';
else
mode1_status(n) = 'Fail';
end
end
mode1_status
mode1_status = 1x11 string array
"Fail" "Fail" "Fail" "Fail" "Fail" "Pass" "Pass" "Pass" "Pass" "Pass" "Pass"
  2 Comments
Stephen23
Stephen23 on 10 Jul 2024
Without any loops:
sigma_f = [118.01 97.84 84.51 67.98 58.13 50.29 45.08 41.36 38.58 36.42 34.70];
m_sigma_yf = 280;
s_factor = 5;
tmp = ["Fail","Pass"];
idx = sigma_f < (m_sigma_yf./s_factor);
out = tmp(1+idx)
out = 1x11 string array
"Fail" "Fail" "Fail" "Fail" "Fail" "Pass" "Pass" "Pass" "Pass" "Pass" "Pass"

Sign in to comment.

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!