Load text file with open braket ( )

5 ビュー (過去 30 日間)
Shan  Chu
Shan Chu 2018 年 3 月 22 日
コメント済み: Birdman 2018 年 3 月 22 日
Dear all,
I have a txt file in the format like:
Then I used the following code:
fid = fopen('ImpedanceMatrix1.txt','r');
A = textscan(fid,'%s');
A = A{:};
However, it doesn't remove the brackets in the variable. Could you please suggest me how to do that. Thanks.
  3 件のコメント
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 2018 年 3 月 22 日
Try this:
  8 件のコメント
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 2018 年 3 月 22 日
編集済み: Stephen23 2018 年 3 月 22 日
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');
C = textscan(fid,'(%f)');
>> C{1}
ans =
7.0036e+001 + 3.2053e+004i
-1.1653e-012 + 2.9239e+004i
-1.4211e-013 + 2.6859e+004i


Help Center および File ExchangeText Data Preparation についてさらに検索


Community Treasure Hunt

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

Start Hunting!

Translated by