how to change a .txt file to a .mat file

32 ビュー (過去 30 日間)
Lucrezia Cester
Lucrezia Cester 2020 年 1 月 31 日
コメント済み: Stephen23 2020 年 1 月 31 日
Hello, I have a .txt file that I want to convert into a .mat file.
I tried this code
M = dlmread('test_background_id.txt'); %Use a better name that M
save('somefile.mat', 'M');
However I get an error
% Error using dlmread (line 147)
% Mismatch between file and format character vector.
% Trouble reading 'Numeric' field from file (row number 1, field number 1) ==> Serial:
% 00002405\n
Anyone knows why this is the case?
Thanks
  2 件のコメント
Bhaskar R
Bhaskar R 2020 年 1 月 31 日
dlmread is expecting format specifier. Can you share some your text file?
Lucrezia Cester
Lucrezia Cester 2020 年 1 月 31 日
Here is the file

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

採用された回答

Stephen23
Stephen23 2020 年 1 月 31 日
編集済み: Stephen23 2020 年 1 月 31 日
"Anyone knows why this is the case?"
dlmread only imports purely numeric data, which your file is not.
You will have to parse the file using some other tools, e.g. using fileread and a regular expression (you could probably coerce textscan and/or readtable to do much the same thing):
>> str = fileread('test_background_id.txt');
>> C = regexpi(str,'^([A-Z]+):\s*(.+?)\s*$','tokens','lineanchors');
>> C = vertcat(C{:})
'Serial' '00002405'
'Commit' 'be6aa3b35e7caab3db1a87f95510bd40c86a7431'
'CCamDeviceVersion' '1.3.0-1920618'
'SystemVersion' '3.0.0'
'SystemBuildDate' 'Mon Oct 08 17:12:03 2018'
'SystemVersionControlID' '7e8c3be8'
'SystemId' '15'
which you can easily convert to a convenient structure:
>> S = cell2struct(C(:,2),C(:,1),1);
>> S.Serial
ans =
00002405
>> S.SystemVersion
ans =
3.0.0
After that it is trivial to save it as a .mat file:
>> save('somefile.mat','-struct','S')
TIP: always load into an output variable:
S = load(...)
  6 件のコメント
Lucrezia Cester
Lucrezia Cester 2020 年 1 月 31 日
where did you attach the .mat file?
Stephen23
Stephen23 2020 年 1 月 31 日
"yes I did use the save line but no .mat file appears "
Did you check in the current directory?
"where did you attach the .mat file?"
To my previous comment. You can download it by clicking the link at the top of the comment. Here is a screenshot showing you where the link is:
Capture.PNG

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by