Delimiter problems with =

6 次查看(过去 30 天)
Sergio Vez
Sergio Vez 2019-10-28
评论: Sergio Vez 2019-10-28
Good afternoon,
I'm using the following function:
filename=fullfile('ENTRADA.txt');
fileID=fopen(filename);
C=textscan(fileID,'%s %s','Delimiter','=','MultipleDelimsAsOne',1);
In order to get a 1x2 cell array (one with the name of the data and the other one with the corresponding data). All the names and the data are separated by "=" and that's why I put it as a Delimiter. The problem is that one of the data is "ZR[1].mat.comment=ISO 6336-5 Figure 9/10 (MQ), Core hardness >=25HRC Jominy J=12mm<HRC28". I would only want to make a separation after the first "=" but MATLAB makes it also for the second and the third "=". How could I fix it?
Thanks in advance!
  2 个评论
Stephen23
Stephen23 2019-10-28
@Sergio Vez: please upload a sample file by clicking the paperclip button.
Remember to also fclose the file.

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2019-10-28
编辑:Stephen23 2019-10-28
opt = {'Delimiter','='};
fmt = '%s%[^\n]';
[fid,msg] = fopen('ENTRADA.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
fclose(fid);
Giving
>> C
C =
{50x1 cell} {50x1 cell}
>> C{1}{32}
ans =
ZR[0].mat.comment
>> C{2}{32}
ans =
ISO 6336-5 Figure 9/10 (MQ), Core hardness >=25HRC Jominy J=12mm<HRC28¶
Every line (except the first) seems to end with a pilcrow sign:
Note that you might find the CollectOutput option useful.
  6 个评论
Stephen23
Stephen23 2019-10-28
编辑:Stephen23 2019-10-28
"How could I fix it?"
tmp needs to be a 2xN cell array, where:
  • row 1 contains the 1st column
  • row 2 contains the 2nd column
Probably something like this would work:
tmp = [Nombres(:),Valores(:)].';
Sergio Vez
Sergio Vez 2019-10-28
Perfect! I got it. You're such an expert

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by