Add phrase to string if condition is true

Hey!
I have a probleming coding a string, here is my current code:
int i = 4;
String[] testString = new String[i];
for (int j = 0; j < i; j++)
{
testString[j] = String.valueOf("test"+(j+1));
}
model.set("input", testString);
which is resulting in:
model.set("input", "test1", "test2", "test3");
___________________
Now I want to extend the code and print just an additional testX if a corresponding condition is true and every testX has a different condition with previous defined parameters like:
condition for test1: parameter1<parameter2
condition for test2: parameter2<parameter3
condition for test3: parameter4>parameter3
For example if condition 2 is not true the result should be:
model.set("input", "test1", "test3");
___________________
I would appreciate any help here!!
Thank you!

 Respuesta aceptada

Mitch Lautigar
Mitch Lautigar el 5 de Mayo de 2022

0 votos

int i = 4;
String[] testString = new String[i];
for (int j = 0; j < i; j++)
{
testString[j] = String.valueOf("test"+(j+1));
}
model.set("input", testString);
This code is either C/C++ and I have not seen this syntax used in programming MATLAB. However, in an attempt to help you, the logic i'd suggest is to make sure test_string is actually saving as a string array. C/C++ don't tend to like a variable changing memory size during the code running. See if that helps, and if it doesn't, comment again and i'll see what I can do to further help you.

6 comentarios

Christopher Schoß
Christopher Schoß el 5 de Mayo de 2022
Thank you for your answer!
The Code is for automating my COMSOL Multiphysics Software and this is how I normally get the structure of my code. Means I track my "clicks" in Comsol and save them as a Matlab File. In Matlab I extend this code to automate the model-buildup in comsol.
Do you have a specific vision for coding my problem? Not sure how I can implement your logic
Thank you!
Mitch Lautigar
Mitch Lautigar el 5 de Mayo de 2022
Editada: Walter Roberson el 6 de Mayo de 2022
If I was going to program this in Matlab, it would look something like this.
function [output] = insertnamehere(param1, param2, param3,param4)
output_str = []; %MATLAB does not care about preallocation of memory. It prefers preallocation, but it is not needed
if param1 < param2
output_str = [output_str, "test1"];
elseif param2 < param3
output_str = [output_str, "test2"];
elseif param3 < param4
output_str = [output_str, "test3"];
end
testString = strjoin(output_str,', ');
model.set("input", testString); %There is a MATLAB version of this line you had, but for simplicity, I left it as is. Hope it helps!
Christopher Schoß
Christopher Schoß el 5 de Mayo de 2022
Yeah THANK YOU!
Nearly what I am looking for.
Its currently resulting in 'test1, test2' so not 'test1', 'test2'
How can I add the missing bracket?
Thank you!
testString = strjoin(output_str,', ');
model.set("input", testString);
Remove those lines. Add
testString = cellstr(testString);
model.set("input",testString{:})
Christopher Schoß
Christopher Schoß el 6 de Mayo de 2022
I think you mean add:
testString = cellstr(output_str);
model.set("input",testString{:});
?
Thank you!
Christopher Schoß
Christopher Schoß el 6 de Mayo de 2022
Now I got it:
output = strjoin(output_str,'","');
Thank you :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2021a

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by