textscan syntax help converting file

2 次查看(过去 30 天)
I'm trying to read in a .txt file and convert the contents to an array. The text file is always formatted as follows (with additional items always on new lines):
Array = {
'Item1', 2526976, 8, 14, 11, [224,224,137,80];
'Item2', 2526976, 8, 18, 15, [224,224,137,80];
};
What is the sytax I use to use with textscan so that it loads into the workspace as follows:
  1 个评论
Steven Lord
Steven Lord 2023-12-7
If the file is formatted using MATLAB syntax, rather than trying to read it as data I'd consider renaming it to have the extension .m instead of the extension .txt. If you do that you could run it as a MATLAB script file.

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2023-12-7
Try something like this —
Array = {'Item1', 2526976, 8, 14, 11, [224,224,137,80]
'Item2', 2526976, 8, 18, 15, [224,224,137,80]};
fido = fopen('Test.txt','wt')
fido = 3
for k = 1:size(Array,1)
fprintf(fido, '%s, %d, %d, %d, %d, [%d,%d,%d,%d]\n',Array{k,:});
end
fclose(fido);
type('Test.txt')
Item1, 2526976, 8, 14, 11, [224,224,137,80] Item2, 2526976, 8, 18, 15, [224,224,137,80]
fidi = fopen('Test.txt','rt');
C = textscan(fidi, '%s %d %d %d %d [%d %d %d %d]', 'Delimiter',{','}, 'CollectOutput',1)
C = 1×2 cell array
{2×1 cell} {2×8 int32}
fclose(fidi);
C
C = 1×2 cell array
{2×1 cell} {2×8 int32}
C{1}
ans = 2×1 cell array
{'Item1'} {'Item2'}
C{2}
ans = 2×8
2526976 8 14 11 224 224 137 80 2526976 8 18 15 224 224 137 80
.
  2 个评论
tybo1mos
tybo1mos 2023-12-11
That got me close to what I was looking for, but not quite in the format I need yet. I managed to get the file read into a cell array wtih all string values as follows:
I'm trying to convert this 3x9 array into a 3x6 array, such that:
  • 1st column is still a string value.
  • Columns 2-5 are converted numbers
  • Columns 6-9 are combined as sub arrays in now column 6 as follows:
Star Strider
Star Strider 2023-12-12
This is as close as I can get to that —
Array = {'Item1', 2526976, 8, 14, 11, [224,224,137,80]
'Item2', 2526976, 8, 18, 15, [224,224,137,80]};
fido = fopen('Test.txt','wt');
fido = 3
for k = 1:size(Array,1)
fprintf(fido, '%s, %d, %d, %d, %d, [%d,%d,%d,%d]\n',Array{k,:});
end
fclose(fido);
type('Test.txt')
Item1, 2526976, 8, 14, 11, [224,224,137,80] Item2, 2526976, 8, 18, 15, [224,224,137,80]
fidi = fopen('Test.txt','rt');
C = textscan(fidi, '%s %d %d %d %d [%d %d %d %d]', 'Delimiter',{','}, 'CollectOutput',1)
C = 1×2 cell array
{2×1 cell} {2×8 int32}
fclose(fidi);
C
C = 1×2 cell array
{2×1 cell} {2×8 int32}
C{1}
ans = 2×1 cell array
{'Item1'} {'Item2'}
C{2}
ans = 2×8
2526976 8 14 11 224 224 137 80 2526976 8 18 15 224 224 137 80
T1 = array2table(cell2mat({C{2}(:,1:4)}));
T1 = [addvars(T1,C{1}, 'Before',1) table(cell2mat({C{2}(:,5:8)}),'VariableNames',{'Var5'})]
T1 = 2×6 table
Var1_1 Var1 Var2 Var3 Var4 Var5 _________ _______ ____ ____ ____ ________________________ {'Item1'} 2526976 8 14 11 224 224 137 80 {'Item2'} 2526976 8 18 15 224 224 137 80
.

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by