Dear all,
I have a txt file in the format like:
(70.0362+32052.7j)
(-1.16529e-12+29239.2j)
(-1.42109e-13+26859.3j)
...
Then I used the following code:
fid = fopen('ImpedanceMatrix1.txt','r');
A = textscan(fid,'%s');
fclose(fid);
A = A{:};
However, it doesn't remove the brackets in the variable. Could you please suggest me how to do that. Thanks.

댓글 수: 3

Stephen23
Stephen23 2018년 3월 22일
편집: Stephen23 2018년 3월 22일
Why are you reading a file with numeric data using a string format? Why not just read the numeric data directly using a numeric format? Reading the data as a string and the post-processing (e.g. using regular expressions) is going to be quite inefficient.
Shan  Chu
Shan Chu 2018년 3월 22일
Hi sir, I am not good at Matlab. I used Load before but it doesn't work. Could you please suggest which function that I could use? Thanks
Stephen23
Stephen23 2018년 3월 22일
편집: Stephen23 2018년 3월 22일
@Shan Chu: I did not write that you should use a different function. I wrote that you should import numeric data using a numeric format, not a string format: these are clearly explained in the textscan documentation. Currently you use '%s', which imports data as a string (a char vector), but instead simply import your data using %f, which will be much more efficient than what you do now. Check my answer to see how simple this is.

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

 채택된 답변

Birdman
Birdman 2018년 3월 22일

0 개 추천

Try this:
regexprep(A,{'(',')'},{'',''})

댓글 수: 8

Shan  Chu
Shan Chu 2018년 3월 22일
Thank you sir. it works perfectly
Birdman
Birdman 2018년 3월 22일
You are welcome.
Shan  Chu
Shan Chu 2018년 3월 22일
Btw, I tried to use cell2mat to convert it to math type but it doesn't work. Could you please help?
Just simply
str2double(regexprep(A,{'(',')'},{'',''}))
Shan  Chu
Shan Chu 2018년 3월 22일
I see, The type is string not double. Thanks sir
Birdman
Birdman 2018년 3월 22일
;-)
Stephen23
Stephen23 2018년 3월 22일
See my answer for a more efficient solution.
Birdman
Birdman 2018년 3월 22일
Yes, I saw as usual :)
But it is only a bit of more code in my case, of course your solution is more efficient.

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

추가 답변 (1개)

Stephen23
Stephen23 2018년 3월 22일
편집: Stephen23 2018년 3월 22일

0 개 추천

It is much more efficient to import the data using a numeric format (rather than as a string and doing post-processing as you are doing now). My test file is attached (you did not provide a sample file).
[fid,msg] = fopen('temp5.txt','rt');
assert(fid>=3,msg)
C = textscan(fid,'(%f)');
fclose(fid);
Giving:
>> C{1}
ans =
7.0036e+001 + 3.2053e+004i
-1.1653e-012 + 2.9239e+004i
-1.4211e-013 + 2.6859e+004i

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2018년 3월 22일

댓글:

2018년 3월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by