이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
how to convert .txt file data to a matrix form
조회 수: 2 (최근 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에서 Logical에 대해 자세히 알아보기
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 (한국어)