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 日
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
Walter Roberson
2019 年 11 月 1 日
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 ?
Image Analyst
2019 年 11 月 1 日
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 ?
Walter Roberson
2019 年 11 月 4 日
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} ?
Walter Roberson
2019 年 11 月 5 日
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 で Data Type Conversion についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
