How di I make concise multiple parameter calls?

조회 수: 1 (최근 30일)
Brantosaurus
Brantosaurus 2022년 9월 30일
댓글: Brantosaurus 2022년 9월 30일
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
Brantosaurus
Brantosaurus 2022년 9월 30일
Yes, i believe so. Got an excellent answer from this valable community.
Brantosaurus
Brantosaurus 2022년 9월 30일
pardon ... valuable community

댓글을 달려면 로그인하십시오.

채택된 답변

Karim
Karim 2022년 9월 30일
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
Brantosaurus
Brantosaurus 2022년 9월 30일
That's absolutely perfect. Curly brackets are a first for me!
Thank you very much Karim. :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 ThingSpeak에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by