フィルターのクリア

Conversion String to Integer with Positive or Negative

9 ビュー (過去 30 日間)
Amanda
Amanda 2012 年 8 月 19 日
I have a 1 X 20 column vector of string data in a file name A.txt.
%Input in A.txt:
60N
61N
50S
51S
Objective: to read the column of data as integers and represent N as positive and S as negative.
So the output would be:
60
61
-50
-51
I have researched other posts, apply str2num double. I'm totally stuck.
Thanks,
Amanda

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 19 日
編集済み: Azzi Abdelmalek 2012 年 8 月 19 日
c=char('60N', '61N', '50S', '51S')
cc=[c(:,end) c(:,1:end-1)]
result=cellfun (@(x) regexprep(x,'N','-'),cellstr(cc),'uni',false)
result=cellfun (@(x) regexprep(x,'S',''),result,'uni',false)
result=str2num(char(result))

その他の回答 (1 件)

Oleg Komarov
Oleg Komarov 2012 年 8 月 19 日
% Import data
fid = fopen('C:\Users\Oleg\Desktop\test.txt');
out = textscan(fid, '%2f%s','Delimiter','');
fclose(fid);
% Recognize positive and negative
data = out{1} .* (strcmp('N',out{2})*2-1);

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by