How would I take the fprintf statement and values and input into a matrix?

1 回表示 (過去 30 日間)
Collin Noblet
Collin Noblet 2018 年 4 月 16 日
回答済み: KSSV 2018 年 4 月 16 日
data = fopen('PH.csv');
pH = textscan(data,'%s %d','delimiter',',');
fclose(data);
rows = size(pH{1});
name = pH{1};
pH = pH{2};
for i = 1:rows
if pH(i) > 7
acid_base{i} = 'It is a base';
elseif pH(i) < 7
acid_base{i} = 'It is an acid';
else
acid_base{i} = 'It is neutral';
end
end
for i = 1:rows
fprintf('%s - pH: %d \n %s\n',name{i},pH(i),acid_base{i});
end
data = fopen('PH.csv');
pH = textscan(data,'%s %d','delimiter',',');
fclose(data);
rows = size(pH{1});
name = pH{1};
pH = pH{2};
for i = 1:rows
if pH(i) > 7
acid_base{i} = 'It is a base';
elseif pH(i) < 7
acid_base{i} = 'It is an acid';
else
acid_base{i} = 'It is neutral';
end
end
for i = 1:rows
fprintf('%s - pH: %d \n %s\n',name{i},pH(i),acid_base{i});
end
  2 件のコメント
KSSV
KSSV 2018 年 4 月 16 日
What you want to do?
Collin Noblet
Collin Noblet 2018 年 4 月 16 日
Instead of displaying the name, pH, and acidity using a fprintf statement, how would I display it in an array?

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

回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 4 月 16 日
Use sprintf to create formatted character vectors that you can store for later use.

KSSV
KSSV 2018 年 4 月 16 日
pH = 1:14 ;
S = cell(length(pH),1) ;
for i = 1:length(pH)
    val = pH(i) ;
    if val < 7
        S{i} = sprintf('pH = %f,solution is acidic',val) ;
    elseif val ==7
        S{i} =  sprintf('pH = %f,solution is neutral',val) ;
    elseif val > 7
        S{i} =  sprintf('pH = %f,solution is basic',val) ;
    end
end

カテゴリ

Help Center および File ExchangeDeep Learning Toolbox についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by