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.
Sergio Vez
Sergio Vez 2019년 10월 28일
Here you have it. Thanks!

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2019년 10월 28일
편집: Stephen23 2019년 10월 28일

1 개 추천

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

Sergio Vez
Sergio Vez 2019년 10월 28일
Thanks you so much for your perfect and quick answer!!
Sergio Vez
Sergio Vez 2019년 10월 28일
One more question.
Once I textscanned the ".txt" and got a (1x2) cell with each of then a 4956x1 cell, I made the modifications I needed and now I want to join them again and save it into a ".txt" again. So:
  • I need to include the "=" again between both cells
  • I need to save it into a ".txt"
How can I do it?
Big thanks in advance again
Sergio
Stephen23
Stephen23 2019년 10월 28일
Something like this should get you started:
tmp = [C{:}].';
[fid,msg] = fopen('new.txt','wt');
assert(fid>=3,msg)
fprintf(fid,'%s=%s\n',tmp{:});
fclose(fid);
Sergio Vez
Sergio Vez 2019년 10월 28일
I tried that but I got this error ("Brace indexing is not supported for variables of this type. Error in Lectura_archivo (line 41) fprintf(fileID,'%s=%s\n',tmp{:});")
I send you the file. I belive the mistakes comes from here:
  • My textscan goes to a variable called "c_aux". (1x1 cell).
  • Due to my needs I need to get separetely both columns so I extract "c" from "c_aux" as C=C_aux{1}; and then the 2 columns from C to Nombres = C(:,1); and Valores = C(:,2);
  • After operating I put together again both columns as C=[Nombres Valores]; and I tried to fprint that.
  • The problem is that C is a 4956x2 cell and not 1x1 cell like "c_aux".
How could I fix it?
Thanks
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개)

카테고리

도움말 센터File Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품

질문:

2019년 10월 28일

댓글:

2019년 10월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by