how to convert .txt file data to a matrix form
4 次查看(过去 30 天)
显示 更早的评论
I have a text file 'MatK.txt'(extracted from ANSYS as stiffness matrix) which has data as follows:
MATK:
[1,1]: 2.250e+007 [1,3]:-1.688e+007 [1,4]: 1.125e+007
[2,2]: 1.500e+009 [2,5]:-7.500e+008
[3,3]: 3.375e+007 [3,6]: 1.688e+007
[4,4]: 4.500e+007 [4,6]: 1.125e+007
[5,5]: 7.500e+008
[6,6]: 2.250e+007
in which the value inside [ , ] represents row and column number. since it is a symmetric matrix it only shows the upper triangular part. I want the full 6x6 matrix.
Kindly help me out. Thanks in advance
采纳的回答
Stephen23
2015-3-30
编辑:Stephen23
2015-4-1
It is not clear exactly what your question is: are you having difficulty reading this data from the file, or converting the text data into a matrix, or filling the matrix from the upper triangle data given? Below is some code that should cover most of these topics. First we define the given data as a string (is this how you read it from the file?):
>> str = 'MATK: [1,1]: 2.250e+007 [1,3]:-1.688e+007 [1,4]: 1.125e+007 [2,2]: 1.500e+009 [2,5]:-7.500e+008 [3,3]: 3.375e+007 [3,6]: 1.688e+007 [4,4]: 4.500e+007 [4,6]: 1.125e+007 [5,5]: 7.500e+008 [6,6]: 2.250e+007';
>> tok = regexp(str,'\[(\d),(\d)\]: ?(\S+)','tokens');
>> mat = cellfun(@(s)sscanf(s,'%f'),vertcat(tok{:}));
>> out = zeros(max(mat(:,1)),max(mat(:,2)));
>> ind = sub2ind(size(out),mat(:,1),mat(:,2));
>> out(ind) = mat(:,3)
out =
1.0e+009 *
0.0225 0 -0.0169 0.0113 0 0
0 1.5000 0 0 -0.7500 0
0 0 0.0338 0 0 0.0169
0 0 0 0.0450 0 0.0113
0 0 0 0 0.7500 0
0 0 0 0 0 0.0225
>> out = triu(out) + triu(out,1).'
out =
1.0e+009 *
0.0225 0 -0.0169 0.0113 0 0
0 1.5000 0 0 -0.7500 0
-0.0169 0 0.0338 0 0 0.0169
0.0113 0 0 0.0450 0 0.0113
0 -0.7500 0 0 0.7500 0
0 0 0.0169 0.0113 0 0.0225
>> tok = cell2mat(textscan(str(6:end),'[%f,%f]:%f'))
30 个评论
Kamal Bera
2015-3-30
thank you very much for your quick response. it will serve my purpose. but I want to directly call the text file, instead of writing str = 'MATK: [1,1]: 2.250e+007 [1,3]:-1.688e+007 [1,4]: 1.125e+007 [2,2]: 1.500e+009 [2,5]:-7.500e+008 [3,3]: 3.375e+007 [3,6]: 1.688e+007 [4,4]: 4.500e+007 [4,6]: 1.125e+007 [5,5]: 7.500e+008 [6,6]: 2.250e+007';
I am attaching the txt file also.
Again thanks a lot.
Stephen23
2015-3-30
编辑:Stephen23
2015-3-30
If this is the only data contained within the file, then you can very easily read the whole file into one string using fileread:
str = fileread(filename);
If the file is more complicated than this, then please try attaching the file again, this time clicking "Attach File" in the bottom right corner of the textbox.
Kamal Bera
2015-3-30
also I observed the -ve values are not considering. e.g. what about [1,3]:-1.688e+007 . It is shown as zero in the final out matrix.
please clarify.
Kamal Bera
2015-3-31
can you kindly explain me how to use str = fileread(filename);
suppose my file name is abcd.txt
Stephen23
2015-3-31
I'm glad to help. You can also Accept an Answer that has helped to resolve your question.
Kamal Bera
2015-4-1
It is not working for another file (file attached). It shows error as follows
Error using cellfun Input #2 expected to be a cell array, was double instead.
Error in Kmatrix (line 6) tok = cellfun(@(s)sscanf(s,'%f'),vertcat(tok{:}));
Stephen23
2015-4-1
编辑:Stephen23
2015-4-1
Please check that you have called both of these lines:
>> tok = regexp(str,'\[(\d),(\d)\]: ?(\S+)','tokens');
>> mat = cellfun(@(s)sscanf(s,'%f'),vertcat(tok{:}));
note that my previous edit renamed the output of the second line from tok to mat: please update your entire code using my (edited) answer.
Kamal Bera
2015-4-1
Still showing the same error.Here are my entire code. Again I am attaching the MatK.txt file.
str = fileread('MatK.txt'); % txt file name MatK extracted from ANSYS
tok = regexp(str,'\[(\d),(\d)\]: ?(\S+)','tokens');
mat = cellfun(@(s)sscanf(s,'%f'),vertcat(tok{:}));
out = zeros(max(mat(:,1)),max(mat(:,2)));
ind = sub2ind(size(out),mat(:,1),mat(:,2));
out(ind) = mat(:,3)
out = triu(out) + triu(out,1).'
Stephen23
2015-4-1
编辑:Stephen23
2015-4-1
Please try attaching the file again, this time clicking Attach File in the bottom right corner of the textbox. Although you have written three times that you have attached the file, you have never actually succeeded in doing this. I suggest that you carefully read the instructions again, and this time both choose and attach the file. It will be clear when you do this correctly, as it appears as a link at the bottom of your comment.
On a similar note, please always format your code using the {} Code button, as what you are writing is a pain to read and difficult to copy to a textfile.
Kamal Bera
2015-4-1
str = fileread('MatK.txt'); % txt file name MatM extracted from ANSYS
tok = regexp(str,'\[(\d),(\d)\]: ?(\S+)','tokens');
mat = cellfun(@(s)sscanf(s,'%f'),vertcat(tok{:}));
out = zeros(max(mat(:,1)),max(mat(:,2)));
ind = sub2ind(size(out),mat(:,1),mat(:,2));
out(ind) = mat(:,3)
out = triu(out) + triu(out,1).'
Stephen23
2015-4-1
编辑:Stephen23
2015-4-1
The problem is quite simple: your example in the original question did not include spaces inside the indices, e.g [1,3]. However the actual datafile includes spaces, e.g. [ 1, 3].
The solution is simply to allow for one space character in front of each digit:
tok = regexp(str,'\[ ?(\d), ?(\d)\]: ?(\S+)','tokens');
If there might be more than one space then you can use an asterisk instead of a question mark. You can read about regular expressions and regexp to know how this works.
Kamal Bera
2015-4-1
Now it is working. I have another question.If a text file (attached)in which corresponding to each row all the column values are given e.g.
ROW 1 NODE 1 DEG. OF. FR. = ROTZ
1 0.13513535E+09 2 0.00000000E+00 3 0.91507762E+08 4-0.59130175E+08
5 0.00000000E+00 6 0.00000000E+00 7 0.00000000E+00 8-0.36603105E+09
9 0.00000000E+00 10 0.00000000E+00 11 0.00000000E+00 12 0.10701035E+09
13 0.00000000E+00 14 0.00000000E+00 15 0.00000000E+00 16 0.00000000E+00
17 0.00000000E+00 18 0.00000000E+00
ROW 2 NODE 2 DEG. OF. FR. = UX
1 0.00000000E+00 2 0.35000101E+10 3 0.00000000E+00 4 0.00000000E+00
5 0.25000125E+09 6 0.00000000E+00 7-0.20000063E+10 8-0.33333439E+06
9 0.00000000E+00 10 0.00000000E+00 11 0.00000000E+00 12 0.00000000E+00
13-0.20000063E+10 14 0.33333439E+06 15 0.00000000E+00 16 0.00000000E+00
17 0.00000000E+00 18 0.00000000E+00
and so on upto 18 rows ( Please see the attached file for clarity ). What modification I need to do in my code to get a 18x18 matrix.
Again thank you for your quick responses. It will help me to progress further in my work.
Kamal Bera
2015-4-15
Hi Mr. Stephen Cobeldick, I am trying to apply your code (see below )to get a matrix of size 1710x1710 from a txt file (attached) as I did earlier to get 6x6 and 18x18 matrices from txt files. Unfortunately it is not working and shows error as
Subscripted assignment dimension mismatch.
Error in KSUBmatrix (line 9)
out(k,:) = val{k}(:,2);
Kindly help me so that I can apply the code for any matrix dimension I wish to extract.It is urgently required.
str = fileread('STIFFNESS MATRIX .txt'); % txt file name STIFFNESS MATRIX extracted from ANSYS
[tok,idx] = regexp(str,'ROW\s+(\d+)\s+NODE\s+(\d+)\s+[^\n=]+=\s+(\w+)','tokens','end');
tok = vertcat(tok{:});
idx = [1+idx,numel(str)];
fun = @(b,e)reshape(sscanf(str(b:e),'%f'),2,[]).';
val = arrayfun(fun,idx(1:end-1),idx(2:end),'UniformOutput',false);
out = zeros(numel(val));
for k = 1:numel(val)
out(k,:) = val{k}(:,2);
end
K=out; % required structural stiffness matrix
The file attached is in zip form as it is of large size. If you required the file corresponding to 6x6 or 18x18 matrix cases(for which the given code works nicely), I will attach those separately.
I am egerly waiting for your quick response.
thank you.
Kamal Bera
2015-4-18
Now I am attaching it in two parts(part-I and part-II).Just copy the second part and paste at the end of first part. also let me know whether you got the txt file or not.Here is part-I.
Kamal Bera
2015-4-19
Hi Mr. Stephen Cobeldick have got the txt file in two part.Kindly help me. It is urgently required. thank you.
Stephen23
2015-4-20
@Kamal Bera: can you please provide this data as one textfile, not zipped, exactly as it is.
Stephen23
2015-4-20
This is a public website: you might like to remove your email address :)
Okay, the zipped data is fine.
Stephen23
2015-4-20
编辑:Stephen23
2015-4-21
Please do not mix your questions, it makes it harder for everyone to keep track of, and to know what information you have provided and what answers you have been given.
This question resolves how to read data with this format:
[1,1]: 2.250e+007 [1,3]:-1.688e+007 [1,4]: 1.125e+007
1 0.13513535E+09 2 0.00000000E+00 3 0.91507762E+08
which matches exactly the data format of the data in the zip files. These are different ways of storing data, and therefore they require different solutions for importing them, which correspond to each question.
It is easier for everyone if you keep your questions focused on one topic, and so you should expand that question (which already deals with that exact format) rather than introduce a new format to this question.
Kamal Bera
2015-4-20
I think I am not able to explain you properly.The answer I have accepted is working nicely for the 18x18 matrix (the link you attached right now). But the problem is that the same code is not working for similar type of large text file (which I attached in two part in zipped form),from which I should get a 1710x1710 size matrix.If you still want me to ask this as a different(new) question, I have no problem. But believe me , I am not able to extract the required 1710x1710 size matrix which is required for further progress in my work. I have tried all the corresponding codes you proposed. Please suggest.
Kamal Bera
2015-4-21
Sorry for mixing up two separate questions. You are right that my other question deals with data in this format:( from this point I will only talk about the following format ).
1 0.13513535E+09 2 0.00000000E+00 3 0.91507762E+08
which matches exactly the data format of the data in the zip files. But the answer you given to import matrix from this type of text file is working nicely for 18x18 matrix, but unfortunately not working for 1710x1710 case (text file send as a zip file). Now my question is how to get the 1710x1710 matrix from the zip file I send.Also the answer should not only work for 1710x1710 case but for all sizes ( obviously the text format will remain the same ). _Kindly tell me if I need to ask this as a new question._I am waiting for your reply.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)