Borrar filtros
Borrar filtros

How di I make concise multiple parameter calls?

1 visualización (últimos 30 días)
Brantosaurus
Brantosaurus el 30 de Sept. de 2022
Comentada: Brantosaurus el 30 de Sept. de 2022
Is there an easy way of wrapping up multiple parameter calls via a single entity?
The parameters are a mix of floating point, integer and characters. I have:
formatspec=['\nExit conditions\n',...
'---------------\n',...
'Total products: %d\n',...
'Maximum fraction: %f\n',...
'Species: %d\n',...
'Product: %s\n',...
'Pressure [bar]: %f\n',...
'Ratio: %f\n\n'];
fprintf(formatspec,nproducts,mfrac,species,name,pcx,ofx);
I would like something on the lines of:
params=???nproducts,mfrac,species,name,pcx,ofx???;
fprintf(formatspec,params);
  3 comentarios
Brantosaurus
Brantosaurus el 30 de Sept. de 2022
Yes, i believe so. Got an excellent answer from this valable community.
Brantosaurus
Brantosaurus el 30 de Sept. de 2022
pardon ... valuable community

Iniciar sesión para comentar.

Respuesta aceptada

Karim
Karim el 30 de Sept. de 2022
One way to do this is by storing the data (or params as you call them) in a cell array, see below for an example.
MyString = ['\nExit conditions\n',...
'---------------\n',...
'Total products: %d\n',...
'Maximum fraction: %f\n',...
'Species: %d\n',...
'Product: %s\n',...
'Pressure [bar]: %f\n',...
'Ratio: %f\n\n'];
% store the params in a cell array
MyParams = {int32(3), pi, 10, "blabla", rand(1)*10, rand(1)}
MyParams = 1×6 cell array
{[3]} {[3.1416]} {[10]} {["blabla"]} {[4.8122]} {[0.0965]}
% print the data
fprintf(MyString,MyParams{:})
Exit conditions --------------- Total products: 3 Maximum fraction: 3.141593 Species: 10 Product: blabla Pressure [bar]: 4.812172 Ratio: 0.096498
  1 comentario
Brantosaurus
Brantosaurus el 30 de Sept. de 2022
That's absolutely perfect. Curly brackets are a first for me!
Thank you very much Karim. :)

Iniciar sesión para comentar.

Más respuestas (0)

Comunidades de usuarios

Más respuestas en  ThingSpeak Community

Categorías

Más información sobre ThingSpeak en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by