필터 지우기
필터 지우기

How to use fprintf?

조회 수: 1 (최근 30일)
Nawal
Nawal 2013년 7월 31일
For example if I have seven variable x=[a;b;c;d;e;f;g]
and I have y=[1;2;3;4;5;6;7]
how can I use fprintf to assign each number from y to its variable in x
I'm so confused, please help!

채택된 답변

David Sanchez
David Sanchez 2013년 7월 31일
fprintf('a= %g; b= %g; c= %g ',y(1),y(2),y(3)) % extend it until y(6)

추가 답변 (2개)

Nawal
Nawal 2013년 7월 31일
Also, how can I add units to each number For this example let's say kg,kg, kg, m, m, KN, KN
Thanks in advance

Jan
Jan 2013년 7월 31일
The reply is easy: You can't.
On one hand fprintf cannot assign variables. One the other hand, the input x=[a;b;c;d;e;f;g] is unclear: What kind of type should this be? A cell string? And on the third hand, such meta-programming is possible using the weird eval, but this causes so many troubles, that any experienced programmer will recommend to avoid it strictly.
Instead of performing "a = 1" magically, it is much smarter and more efficient to store data in a vector (your y looks fine already!) or in a struct:
fieldList = {'a', 'b', 'c'};
dataList = [1, 2, 3];
for k = 1:length(fieldList)
S.(fieldList{i}) = dataList(i);
end
You can search in the forum for "eval" and "assignin" to find more discussions about this frequently asked topic.

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by