Borrar filtros
Borrar filtros

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

23 visualizaciones (últimos 30 días)
Dwayn Maduro
Dwayn Maduro el 10 de Jul. de 2024 a las 9:33
Comentada: Stephen23 el 10 de Jul. de 2024 a las 11:17
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 comentario
Umar
Umar el 10 de Jul. de 2024 a las 9:43

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.

Iniciar sesión para comentar.

Respuesta aceptada

Aquatris
Aquatris el 10 de Jul. de 2024 a las 9:50
Editada: Aquatris el 10 de Jul. de 2024 a las 9:52
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 comentarios
Dwayn Maduro
Dwayn Maduro el 10 de Jul. de 2024 a las 10:00
This worked beautifully, thank you very much.
Stephen23
Stephen23 el 10 de Jul. de 2024 a las 11:17
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"

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by