Filter löschen
Filter löschen

textscan syntax help converting file

2 Ansichten (letzte 30 Tage)
tybo1mos
tybo1mos am 7 Dez. 2023
Kommentiert: Star Strider am 12 Dez. 2023
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 Kommentar
Steven Lord
Steven Lord am 7 Dez. 2023
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Star Strider
Star Strider am 7 Dez. 2023
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 Kommentare
tybo1mos
tybo1mos am 11 Dez. 2023
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 am 12 Dez. 2023
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
.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by