sprintf

40 ビュー (過去 30 日間)
Katherine Anderson
Katherine Anderson 2011 年 7 月 12 日
回答済み: Steven Lord 2025 年 1 月 28 日
I'm trying to get sprintf to print out a line that says: 'for event ----- stations pass the criteria test=' where it will list the results for my stations vertically like:
U02B.HHZf
U04B.HHZf
U05B.HHZf
U06B.HHZf
and so on. I can't seem to get it to do this..it keeps combining my stations together something like UUU00452648BBBB....HHHHHZZZZZfffff. I'm looking at the help pages but there are few examples, any suggestions? This is what I've written so far:
u=char(files(keep==1));
str1=sprintf('for event %s stations passed criteria
test=%f\n',char(dirs(i)),char(u));
disp(str1)
where u= U02B.HHZf
U04B.HHZf
U05B.HHZf
U06B.HHZf
and dirs(i) corresponds to my directories. Any suggestions or help in what I'm doing wrong?
Kayle

回答 (4 件)

Jan
Jan 2011 年 7 月 12 日
編集済み: Voss 2025 年 1 月 28 日
Perhaps this helps:
Name = {'String1', 'String2', 'String3'};
Data = {1, 2, 3};
C = cat(1, Name, Data);
fprintf('Name is %s, value is %d\n', C{:});
Name is String1, value is 1 Name is String2, value is 2 Name is String3, value is 3

Walter Roberson
Walter Roberson 2011 年 7 月 12 日
Using a for loop for that would be easiest, probably.
Question: what value is it that you are trying to print out with the %f numeric format?

Voss
Voss 2025 年 1 月 28 日
files = {'U02B.HHZf';'U04B.HHZf';'U05B.HHZf';'U06B.HHZf'};
keep = true(size(files));
dirs = {'some\directory'};
i = 1;
tmp = sprintf('%s\n',files{keep==1});
str1 = sprintf('for event %s stations passed criteria test=\n%s',char(dirs(i)),tmp);
disp(str1)
for event some\directory stations passed criteria test= U02B.HHZf U04B.HHZf U05B.HHZf U06B.HHZf

Steven Lord
Steven Lord 2025 年 1 月 28 日
This wasn't an option when the question was originally asked, but in more recent releases I recommend storing the names as a string array.
S = ["U02B.HHZf"
"U04B.HHZf"
"U05B.HHZf"
"U06B.HHZf"]
S = 4x1 string array
"U02B.HHZf" "U04B.HHZf" "U05B.HHZf" "U06B.HHZf"
passed = [true true false true] % 5B didn't pass, the others did
passed = 1x4 logical array
1 1 0 1
sprintf("For event, station passed criteria test=%s\n", S(passed))
ans =
"For event, station passed criteria test=U02B.HHZf For event, station passed criteria test=U04B.HHZf For event, station passed criteria test=U06B.HHZf "
Or to display the header only once:
sprintf("For event, stations passed criteria test=\n%s", sprintf("%s\n", S(passed)))
ans =
"For event, stations passed criteria test= U02B.HHZf U04B.HHZf U06B.HHZf "

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by