fprintf automatic spacing for strings of different lenghts

19 ビュー (過去 30 日間)
Alexandre
Alexandre 2023 年 10 月 16 日
コメント済み: Alexandre 2023 年 10 月 17 日
Hi,
how do i evenly space if I print this text using fprintf, in the example 1 below is what I get but I would like to output in the format of example 2, is there a way to automate the spacing if we the consecutive places and I have a string(name of place n) of different lenghts?
where cities is an array of strings show in the examples below and paths is a vector
for i = 1:(length(path)-1)
fprintf('%s --> %s (Distance = %s km)\n', cities(path(i)), cities(path(i+1)), num2str(matDist(min(... [path(i) path(i+1)]), max([path(i) path(i+1)]))));
end
fprintf('%s --> %s (Distance = %s km)\n', cities(path(end)), cities(path(1)), num2str(matDist(min(... [path(1) path(end)]), max([path(1) path(end)]))));
Example 1
Path and distance
----------------------------------------
Case fileName | Origin = Vila Ló(city 1)
----------------------------------------
Total Distance = 414 km
Vila Ló --> Pião (Distance = 46 km)
Pião --> Chão de Frades (Distance = 73 km)
Chão de Frades --> Salmorrete (Distance = 205 km)
Salmorrete --> Tortolindo (Distance = 33 km)
Tortolindo--> Vila Ló (Distance = 57 km)
Example 2
Path and distance
----------------------------------------
Case fileName | Origin = Vila Ló(place 1)
----------------------------------------
Total Distance = 414 km
Vila Ló --> Pião (Distance = 46 km)
Pião --> Chão de Frades (Distance = 73 km)
Chão de Frades --> Salmorrete (Distance = 205 km)
Salmorrete --> Tortolindo (Distance = 33 km)
Tortolindo --> Vila Ló (Distance = 57 km)
  2 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 16 日
移動済み: Dyuman Joshi 2023 年 10 月 16 日
Vectorized version -
%Sample data
cities1 = ["Vila Ló", "Pião", "Chão de Frades", "Salmorrete", "Tortolindo"];
cities2 = cities1([2 3 4 5 1]);
distance = [46 73 205 33 57];
%Convert the distance to strings with the desired precision
distance = compose("%d", distance);
n = repelem(max(strlength(cities1)),1,numel(cities1));
%If you need to add more spaces in between, modify n accordingly
%For e.g
%n = n+5;
fprintf('%-*s --> %-*s (Distance = %s km)\n', [n;cities1;n;cities2;distance])
Vila Ló --> Pião (Distance = 46 km) Pião --> Chão de Frades (Distance = 73 km) Chão de Frades --> Salmorrete (Distance = 205 km) Salmorrete --> Tortolindo (Distance = 33 km) Tortolindo --> Vila Ló (Distance = 57 km)
Alexandre
Alexandre 2023 年 10 月 17 日
Thank you!

サインインしてコメントする。

採用された回答

Voss
Voss 2023 年 10 月 16 日
cities = ["Pasadena","Urbana-Champaign","Cambridge"];
path = 1:numel(cities);
d = [3200 1600 4800]; % using a vector of distances since I don't have your function matDist()
n = max(strlength(cities));
for i = 1:(length(path)-1)
fprintf('%-*s --> %-*s (Distance = %.f km)\n', n, cities(path(i)), n, cities(path(i+1)), d(i));
end
Pasadena --> Urbana-Champaign (Distance = 3200 km) Urbana-Champaign --> Cambridge (Distance = 1600 km)
fprintf('%-*s --> %-*s (Distance = %.f km)\n', n, cities(path(end)), n, cities(path(1)), d(end));
Cambridge --> Pasadena (Distance = 4800 km)
  3 件のコメント
Voss
Voss 2023 年 10 月 16 日
You're welcome!
Walter Roberson
Walter Roberson 2023 年 10 月 16 日
Using the %* facility is a bit obscure, but should work fine for this purpose.

サインインしてコメントする。

その他の回答 (1 件)

Florian Bidaud
Florian Bidaud 2023 年 10 月 16 日
編集済み: Florian Bidaud 2023 年 10 月 16 日
Something like this should do
cities = ["Vila Lo" "Piao" "Chao de Frades" "Salmorrete" "Tortolindo"];
path = [1 2 3 4 5];
distance = [46 73 205 3357]; % Should be replaced by your function matDist
max_city_len = max(strlength(cities));
for i = 1:(length(path)-1)
fprintf('%s %s --> %s %s (Distance = %s km)\n', cities(path(i)),repmat(' ',1,max_city_len-strlength(cities(path(i)))+1), repmat(' ',1,max_city_len-strlength(cities(path(i+1)))+1), cities(path(i+1)), num2str(distance(i)));
end
Vila Lo --> Piao (Distance = 46 km) Piao --> Chao de Frades (Distance = 73 km) Chao de Frades --> Salmorrete (Distance = 205 km) Salmorrete --> Tortolindo (Distance = 3357 km)
fprintf('%s %s --> %s %s (Distance = %s km)\n', cities(path(end)), repmat(' ',1,max_city_len-strlength(cities(path(end)))+1), repmat(' ',1,max_city_len-strlength(cities(path(1)))+1), cities(path(1)), num2str(distance(end)));
Tortolindo --> Vila Lo (Distance = 3357 km)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by