Copy with Matlab

1 回表示 (過去 30 日間)
Alessandro Innocenti
Alessandro Innocenti 2012 年 6 月 21 日
Hi everyone!
I have this problem: I have a big data list in .txt file, with three columns and various lines; a variable number of lines constitutes a series. At the end of each series, there is a blank line. Then another series starts.
i.e.
20 23 103
98 23 92
22 11 118
19 32 110
(blank line)
15 15 22
11 33 33
55 55 55
98 22 22
(blank line)
43 44 23
33 33 44
40 33 99
12 23 43
...and so on. I would like to copy each series near the previous series. The copy of each series should end when it reaches the blank line.
i.e.
20 23 103 15 15 22 43 44 23
98 23 92 11 33 33 33 33 44
22 11 118 55 55 55 40 39 99
19 32 110 98 22 22 12 23 43
I have a Macro doing this in Excel and I can post it if you want, but I would like to do it in Matlab. Any idea? Thank you,
Ale
  2 件のコメント
Walter Roberson
Walter Roberson 2012 年 6 月 21 日
Are the series always equal length?
Alessandro Innocenti
Alessandro Innocenti 2012 年 6 月 21 日
No, they aren't. Or better, I have many .txt files: in the same file .txt all series are always equal lenght, but different .txt files have series that are different lenght.

サインインしてコメントする。

回答 (4 件)

Macko
Macko 2012 年 6 月 21 日
Hi,
I'd probably be writting a Perl script that I then execute either on its own or from within the Matlab environment. I think the systax from within Matlab would be !!your_perl_script.pl.The Perl script would than parse the file and reformt it.
The reason I'd go for Perl in this case is because it has some very powerful string handling features (think regular expressions).
Hope this helps!
  1 件のコメント
Alessandro Innocenti
Alessandro Innocenti 2012 年 6 月 21 日
I'm sorry, I didn't understand. Have you written the Perl Script yet? Or have you been writting?
Where could I find this script? How can I use it (I don't know what a Perl Script is)?
Thank you very much for your help.

サインインしてコメントする。


Alessandro Innocenti
Alessandro Innocenti 2012 年 6 月 22 日
I'm sorry for my craving, but I really need help. Any idea? Thank you very much! Ale

Christoph
Christoph 2012 年 6 月 22 日
Well,
I'm not used to reading .txt files in Matlab, but there is a way I guess. I would read the entire file as one string and replace two newline commands with a temporary string. Next step is deleting the newline which are left. Final step is replacing the temporary string with one newline.
I know there might be better solutions, but this one should be pretty easy to implement. Just start reading the documentation files to this functions: - regexprep - fscanf
good luck, CN

Walter Roberson
Walter Roberson 2012 年 6 月 22 日
inlines = {};
fid = fopen('YourFile.txt', 'r');
while true
K = 0;
thisline = fgetl(fid);
if ~ischar(thisline); break; end %end of file
if length(deblank(thisline)) == 0; continue; end; %end of this series
K = K + 1;
if length(inlines) < K; inlines{K} = []; end
inlines{K} = [inlines{K} fscanf('%f', thisline)];
end
fclose(fid);
seriesdata = cell2mat(inlines);
  4 件のコメント
Alessandro Innocenti
Alessandro Innocenti 2012 年 7 月 4 日
編集済み: Alessandro Innocenti 2012 年 7 月 5 日
Ok, if I change fscanf with sscanf the program runs...but I've now another problem. The program runs but...where can I see the results? As I have written before,I would like to copy each series near the previous series in a new file, but the program doesn't show me this result. Indeed, "inlines" is empty.
Thank you for your help! Ale
Alessandro Innocenti
Alessandro Innocenti 2012 年 7 月 5 日
Why the "inlines" matrix is empty?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by