필터 지우기
필터 지우기

textscan syntax help converting file

조회 수: 2 (최근 30일)
tybo1mos
tybo1mos 2023년 12월 7일
댓글: Star Strider 2023년 12월 12일
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개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by