Hi!
I am trying to read an output from a software that gives me numbers and semicolons, commas, minus and points. The output can be either an excel file ou txt file. If I choose the excel file and open it in matlab, the output is like this:
1
9
.
3
1
2
5
COMMA
2
5
.
1
2
5
0
COMMA
-
1
3
.
7
But I want to matlab recognize if it is a number or a character. In reality, what I want to do is to have "19.3125, 25.1250, -13.7,..." and for that I need matlab to recognize what is a number and not. Can you help me?

5 件のコメント

Jan
Jan 2018 年 8 月 9 日
編集済み: Jan 2018 年 8 月 9 日
What does "open the file in Matlab" exactly mean? By fopen, in the editor, imported by fileread or importdata? Does "COMMA" mean a comma or literally the string 'COMMA'? Are there linebreaks after each character?
Stephen23
Stephen23 2018 年 8 月 9 日
@Patrícia Falcão: please upload a sample text file by clicking the paperclip button.
Patrícia Falcão
Patrícia Falcão 2018 年 8 月 9 日
It means I imported to matlab, there is an image. COMMA means ",". I want to create a for cycle and then, whenever "C" appears, I want to transform it into a new column.
Stephen23
Stephen23 2018 年 8 月 9 日
編集済み: Stephen23 2018 年 8 月 9 日
'COMMA means ","'
Sure, we know what "comma" means. But which of these is actually in the text file?:
  1. ,
  2. COMMA
This would be much simpler if you just uploaded a sample text file by clicking on the paperclip button.
Patrícia Falcão
Patrícia Falcão 2018 年 8 月 9 日
You're right. It is uploaded the txt file. But I don't want the first numbers, just the next to the first comma. What I have already explained to Stephen Cobeldick.

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

 採用された回答

Stephen23
Stephen23 2018 年 8 月 9 日
編集済み: Stephen23 2018 年 8 月 9 日

0 投票

Using a simple text file (this will be much easier than processing an Excel file):
>> str = fileread('temp0.txt');
>> str = regexprep(str,'\s+','');
>> vec = sscanf(str,'%fCOMMA',[1,Inf])
vec =
19.312 25.125 -13.700
The test file is attached. If you had actually uploaded your file then I could have tested my code on that: no sample data means I have to invent my own data, which may or may not be the same as what you have.

4 件のコメント

Patrícia Falcão
Patrícia Falcão 2018 年 8 月 9 日
That works perfectly! The only problem is that the output in txt file is another one:
%
Time [s],Value,Parity Error,Framing Error
0.050669000000000,1,,
0.050754000000000,9,,
0.050840000000000,.,,
0.050925000000000,3,,
0.051010000000000,1,,
0.051095000000000,2,,
0.051180000000000,5,,
0.051265000000000,COMMA,,
0.051350000000000,2,,
0.051435000000000,5,,
0.051520000000000,.,,
0.051605000000000,1,,
0.051690000000000,2,,
0.051775000000000,5,,
0.051860000000000,0,,
0.051945000000000,COMMA,,
0.052030000000000,1,,
0.052115000000000,3,,
0.052200000000000,.,,
0.052285000000000,7,,
0.052370000000000,5,,
0.052455000000000,0,,
0.052540000000000,0,,
0.052625000000000,COMMA,,
0.052710000000000,3,,
0.052795000000000,9,,
0.052880000000000,.,,
0.052965000000000,8,,
0.053050000000000,7,,
0.053135000000000,5,,
0.053220000000000,0,,
0.053305000000000,COMMA,,
0.053390000000000,0,,
0.053475000000000,.,,
0.053560000000000,8,,
0.053645000000000,1,,
0.053730000000000,2,,
0.053815000000000,5,,
0.053900000000000,COMMA,,
0.053985000000000,-,,
0.054070000000000,1,,
0.054155000000000,.,,
0.054240000000000,5,,
0.054325000000000,6,,
0.054410000000000,2,,
0.054495000000000,5,,
0.054580000000000,;,,
0.054665000000000,\r,,
0.054750000000000,\n,,
0.156414000000000,1,,
0.156499000000000,9,,
0.156584000000000,.,,
0.156669000000000,3,,
0.156754000000000,1,,
0.156839000000000,2,,
0.156924000000000,5,,
0.157009000000000,COMMA,,
I don't want the first number (time), I just want the next char/number next to the first comma. In this case, my final goal is to have something like that: 19.3125, 25.1250, 13.7500, 39.8750, 0.8125, -1.5625; (then a new line) 19.3125, ...
Stephen23
Stephen23 2018 年 8 月 9 日
編集済み: Stephen23 2018 年 8 月 9 日
You can read the second column of that text file like this:
>> fmt = '%*s%s%*[^\n]'; % keep only second column.
>> opt = {'Delimiter',',','HeaderLines',1};
>> [fid,msg] = fopen('channel3txt.txt','rt');
>> assert(fid>=3,msg)
>> C = textscan(fid,fmt,opt{:}); % read file data into MATLAB
>> fclose(fid);
>> C = strrep(C{1},'COMMA',','); % replace COMMA -> ,
>> str = sprintf([C{:}]) % convert to char vector, and \r\n -> to newline characters.
str = 19.3125,25.1250,13.7500,39.8750,0.8125,-1.5625;
19.3125,25.1250,13.7500,39.8750,0.8125,-1.5625;
19.3125,25.1250,13.7500,39.8750,0.8125,-1.5625;
19.3125,25.1250,13.7500,39.8750,0.8125,-1.5625;
19.3125,25.1250,13.7500,39.8750,0.8125,-1.5625;
... lots of rows here
66.3125,48.6250,165.8750,138.6250,27.8125,175.7500;
57.8125,59.2500,148.1875,1.7500,18.8750,113.4375;
50.0625,62.0000,96.2500,347.9375,2.1875,60.0625;
48.1250,59.5000,92.5000,345.5625,0.6250,63.9375;
59.7500,61.1250,144.8125,2.1875,18.6250,119.5625;
69.3750,46.9375,168.6250,139.4375,21.7500,171.3750;
62.2500,51.3750,164.8750,13
Note that your sample file does not include data for the complete last row, it appears to be truncated.
At this point str is a char vector with numbers separated by commas and semi-colons with newlines. You can efficiently convert that char vector into a numeric vector using:
>> vec = sscanf(str,'%f,%f,%f,%f,%f,%f;')
vec =
19.31250
25.12500
13.75000
39.87500
0.81250
-1.56250
19.31250
25.12500
... lots of rows here:
46.93750
168.62500
139.43750
21.75000
171.37500
62.25000
51.37500
164.87500
13.00000
At that point you need to decide what to do with the incomplete last row. One option is to ignore it, allowing you to reshape the data into this numeric matrix:
>> vec = vec(1:6*fix(numel(vec)/6)); % remove incomplete last row
>> mat = reshape(vec,6,[]).'
mat =
19.31250 25.12500 13.75000 39.87500 0.81250 -1.56250
19.31250 25.12500 13.75000 39.87500 0.81250 -1.56250
19.31250 25.12500 13.75000 39.87500 0.81250 -1.56250
19.31250 25.12500 13.75000 39.87500 0.81250 -1.56250
19.31250 25.12500 13.75000 39.87500 0.81250 -1.56250
19.31250 25.12500 13.75000 39.87500 0.81250 -1.56250
19.31250 25.12500 13.75000 39.87500 0.81250 -1.56250
19.31250 25.12500 13.75000 39.87500 0.81250 -1.56250
... lots of rows here
50.06250 62.00000 96.25000 347.93750 2.18750 60.06250
48.12500 59.50000 92.50000 345.56250 0.62500 63.93750
59.75000 61.12500 144.81250 2.18750 18.62500 119.56250
69.37500 46.93750 168.62500 139.43750 21.75000 171.37500
Patrícia Falcão
Patrícia Falcão 2018 年 8 月 13 日
Thank you very much Stephen Cobeldick! You helped me a lot!
Stephen23
Stephen23 2018 年 8 月 13 日
@Patrícia Falcão: I hope that it helps! Remember to accept the answer that helped you most: this give reputation points to the volunteers who help you on this forum.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by