Import .file values

1 回表示 (過去 30 日間)
Ancalagon8
Ancalagon8 2024 年 4 月 5 日
編集済み: Ancalagon8 2025 年 1 月 6 日
I have a .file I need to import the data into matlab, but i have trouble handling the format.
Any help?

採用された回答

Voss
Voss 2024 年 4 月 5 日
unzip file.zip
ls *.file
file.file
str = fileread('file.file')
str =
'Fri Apr 5 11:51:00 2024 51{'y': -0.06464344482421874, 'x': -0.6272808349609375, 'z': -10.453084448242187} Fri Apr 5 11:52:00 2024 52{'y': -0.07464344482421874, 'x': -0.7272808349609375, 'z': -10.553084448242187}'
C = regexp(str,'([^\r\n]+)\d{2}{''y'':(.+?), ''x'':(.+?), ''z'':(.+?)}','tokens');
C = vertcat(C{:})
C = 2x4 cell array
{'Fri Apr 5 11:51:00 2024 '} {' -0.06464344482421874'} {' -0.6272808349609375'} {' -10.453084448242187'} {'Fri Apr 5 11:52:00 2024 '} {' -0.07464344482421874'} {' -0.7272808349609375'} {' -10.553084448242187'}
t = strtrim(C(:,1))
t = 2x1 cell array
{'Fri Apr 5 11:51:00 2024'} {'Fri Apr 5 11:52:00 2024'}
yxz = str2double(C(:,2:end))
yxz = 2x3
-0.0646 -0.6273 -10.4531 -0.0746 -0.7273 -10.5531
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  5 件のコメント
Voss
Voss 2024 年 4 月 7 日
The code in my answer uses fileread, so I guess you are trying to run the code in my comment, which is for combining multiple files into one. Since you don't have readlines, try this instead:
unzip file.zip
ls *.file
file1.file file2.file
directory = '.'; % directory where your files are
output_file = 'all_files.file'; % output file to write, containing contents of all files
files = dir(fullfile(directory,'*.file'));
files = fullfile({files.folder},{files.name});
N = numel(files);
C = cell(N,1);
for ii = 1:N
C{ii} = regexprep(fileread(files{ii}),'\r?\n$','');
end
fid = fopen(output_file,'w');
fprintf(fid,'%s\n',strjoin(C,newline()));
fclose(fid);
% check the result for these two files
type(output_file)
Fri Apr 5 11:51:00 2024 51{'y': -0.06464344482421874, 'x': -0.6272808349609375, 'z': -10.453084448242187} Fri Apr 5 11:52:00 2024 52{'y': -0.07464344482421874, 'x': -0.7272808349609375, 'z': -10.553084448242187} Fri Apr 5 11:52:00 2024 51{'y': -0.08464344482421874, 'x': -0.6272808349609375, 'z': -10.453084448242187} Fri Apr 5 11:53:00 2024 52{'y': -0.09464344482421874, 'x': -0.7272808349609375, 'z': -10.553084448242187}
Voss
Voss 2024 年 4 月 7 日
C = regexp(str,'([^\r\n]+)\d{2}{''y'':(.+?), ''x'':(.+?), ''z'':(.+?)} {''a'':(.+?), ''b'':(.+?), ''c'':(.+?)}','tokens');

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by