read matrix from txt. files
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi, if I have a txt. file with 2 different matrix in the text. file and i want them to be read into matlab, eg:
this is from the txt. file:
% A =
[ 1 2 3 4
5 6 7 8]
% B =
[9 10 11 12
13 14 15 16]
and i want matlab to know the diffrent between these two element when it reads the file. so i can make them longer or shorter and it should still be able to take both matrix out.
댓글 수: 2
polo Mahmoud
2019년 10월 31일
편집: polo Mahmoud
2019년 10월 31일
i want some how to read where the start of this [ begins and all the numbers between until ] and then the same for the next matrix ?
Or maybe read all numbers from 1 head line to next head line and ect.
Met V
2019년 10월 31일
importdata('filename.txt')
채택된 답변
Walter Roberson
2019년 10월 31일
0 개 추천
S = fileread('YourFile.txt');
parts_text = regexp(S, '\[[\]*\]', 'match');
parts_value = cellfun(@(txt) textscan(txt, '', 'collectoutput', true), parts_text);
parts_value will now (if all went well) be a cell array of numeric arrays, with one entry for each [] delimited block. In this code, it is not required that each block has the same number of rows or columns as the other blocks.
댓글 수: 15
polo Mahmoud
2019년 10월 31일
편집: polo Mahmoud
2019년 11월 1일
i have attatched the text file, but i cant make it work with your code, can you help me with what i'm doing wrong
S = fileread('coord.txt');
parts_text = regexp(S, '(?<=\[).*?(?=\])', 'match');
parts_value = cellfun(@(txt) textscan(txt, '', 'collectoutput', true), parts_text);
polo Mahmoud
2019년 11월 1일
편집: polo Mahmoud
2019년 11월 1일
But can i ask you one last thing, what if the matrix contains letters in some of the rows, eg:
% A =
[ 1 2 3 IX
5 6 7 IX]
% B =
[ 1 2 3 E
5 6 7 E]
can it still find the letters too ?
Is the array a cell array or a string or character array? A cell array is the only data type where you can mix numbers and strings. A string array could hold them if the numbers were actually character strings (like the letters).
What does this say
whos A
whos B
polo Mahmoud
2019년 11월 1일
편집: polo Mahmoud
2019년 11월 1일
Image Analyst, this is the number of matrix i have and i can read almost all of them, but i can't with the last matrix, call 'connec' in the text file.
and my matlab code is this for now:
clear all;clc
S = fileread('coord2.txt');
parts_text = regexp(S, '(?<=\[).*?(?=\])', 'match');
parts_value = cellfun(@(txt) textscan(txt, '', 'collectoutput', true), parts_text);
AAA = cell2mat(parts_value);
nCoord=AAA(:,1:4);
supp=AAA(:,5:10);
SF=AAA(:,11:16);
rho(:,1) = AAA(:,17);
E(:,1) = AAA(:,19);
But i cant read the last one because i think that it contains A and IX.
Walter Roberson
2019년 11월 1일
What do you want to have happen with the letters? Will they only appear at the end of the row?
polo Mahmoud
2019년 11월 4일
i just want them to read, because they are defined in the scribe to a specifik number, but i want to be able to load a matrix contaning the these letters eg.
A = 20;
IX = 10;
and the matrix I load in from the txt file should be able to containig these letters and then, when I click run it should be able to transform the letters in the matrix to these above numbers
Walter Roberson
2019년 11월 4일
Do you have the Symbolic Toolbox?
polo Mahmoud
2019년 11월 4일
편집: Walter Roberson
2019년 11월 4일
yes like if the "user" writes this in the text file eg.
A = 30;
IX = 10;
% A =
[ 1 2 3 IX
5 6 7 IX]
% B =
[ 1 2 3 A
5 6 7 A]
and when you load this txt files in the matlab then i want the matlab to know what the matrix should look like:
% A =
[ 1 2 3 10
5 6 7 10]
% B =
[ 1 2 3 30
5 6 7 30]
Walter Roberson
2019년 11월 4일
Your coord2.txt file does not define the named variables it uses, so you need to define whether the return is to be:
- always a cell array of character vectors for every position, nothing interpreted as numeric; or
- always a cell array, in which all numeric-looking entries and all named variables that resolved to numeric are converted to numeric, and all unresolved character vectors are left as character vectors, but all in all every position is a cell; or
- like above, but with the additional step that if every position resolves to numeric then convert the entire array to numeric, so the code section can return either a numeric array (if everything resolved) or a cell array of mostly numeric but also character vectors if some did not resolve; or
- perhaps the section should error if given a file with unresolved variables
Your current setup appears to permit the user to define variables as arrays and use the variables in other variables. Is that a deliberate feature?
polo Mahmoud
2019년 11월 4일
편집: polo Mahmoud
2019년 11월 4일
yes i want the user to be able to define A and IX and then when imported/loaded into matlab it should only give me the matrix. because right now i have made a code that can read the matrix but when i try to define A and IX in the matrix it gives NaN in thoes places.
Can you show me a code or an example on how to be able to do that ?
named = regexp(S, '^\s*(?<varname>[A-Za-z]+)\s*=\s*(?<value>[^;]*)', 'lineanchors', 'names');
to_replace = cellfun(@(s) ['(?<=\W)' s '(?=\W)'], {replacements.varname}, 'UniformOutput', false);
replace_with = {named.value};
Smod = regexprep(S, {'^\s*[A-Za-z]+.*$', to_replace{:}}, {'', replace_with{:}}, 'lineanchors', 'dotexceptnewline');
parts_text = regexp(Smod, '(?<=\[).*?(?=\])', 'match');
parts_value = cellfun(@(txt) textscan(txt, '', 'collectoutput', true), parts_text);
polo Mahmoud
2019년 11월 5일
what should i write in {replacements.varname} and {named.value}
is it {XI} and {10} ?
named = regexp(S, '^\s*(?<varname>[A-Za-z]+)\s*=\s*(?<value>[^;\n]*)', 'lineanchors', 'names');
to_replace = cellfun(@(s) ['(?<=\W)' s '(?=\W)'], {named.varname}, 'UniformOutput', false);
replace_with = {named.value};
Smod = regexprep(S, {'^\s*[A-Za-z]+.*$', to_replace{:}}, {'', replace_with{:}}, 'lineanchors', 'dotexceptnewline');
parts_text = regexp(Smod, '(?<=\[).*?(?=\])', 'match');
parts_value = cellfun(@(txt) textscan(txt, '', 'collectoutput', true), parts_text);
Sorry, I renamed the variable as I worked so I still had the old variable name in memory so it passed my testing...
{named.varname} and {named.value} are output from the regexp(), so you do not need to fill in anything.
The code finds all replacements of the form
name = value;
(with no % at the beginning of the line) in the file, and records them all first. Their position in the file is not taken into account. If any name is used twice, then the first of them will have effect.
The code then removes those defining lines, and substitutes the replacements textually without any attempt to understand them. If the line were
A = 10 20] 30 40;
then it would substitute '10 20] 30 40' for each occurance of 'A', whether or not it made sense to do so.
After the substitutions are done, it breaks up into parts according to the [ ] and then it runs textscan on each part to try to convert whatever is there into numeric.
polo Mahmoud
2019년 11월 5일
Thank you very much :D
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
참고 항목
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)
