read matrix from txt. files
显示 更早的评论
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.
importdata('filename.txt')
采纳的回答
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.
What do you want to have happen with the letters? Will they only appear at the end of the row?
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
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]
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);
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.
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)
