import txt file in matlab

조회 수: 1 (최근 30일)
BA
BA 2024년 6월 18일
편집: Stephen23 2024년 6월 19일
I failed to import the attached txt which I need it to be read with the below function
which converts it into another data shape. I do not know what seems to be the problem. Any ideas?
function FBW = get_FBW(Ftable)
d = importdata(Ftable);
for i = 2:size(d.data,1) % do each row separately
rowdat = d.data(i,:);
rowdat = rowdat(~isnan(rowdat));
col = (rowdat(1:2:end-1));
val = rowdat(2:2:end);
nframes = col(end);
FBW(i-1,:) = zeros(1,nframes);
for j = 2:length(col)
if col(j) - col(j-1) >0
slope = (val(j) - val(j-1)) ./ (col(j) - col(j-1));
for k = 0:col(j)-col(j-1)
FBW(i-1, (col(j-1)+k)) = val((j-1)) + k*slope;
end
end
end
end

채택된 답변

Image Analyst
Image Analyst 2024년 6월 19일
It looks like just a simple matrix of numbers. Try readmatrix
d = readmatrix(Ftable)
  댓글 수: 3
BA
BA 2024년 6월 19일
and this one for using the code before:Unable to perform assignment because the size of the left side is 1-by-6 and the size of the right side is 1-by-2500.
Error in get_FBW (line 19)
FBW(i-1,:) = zeros(1,nframes);
Stephen23
Stephen23 2024년 6월 19일
편집: Stephen23 2024년 6월 19일
d = readmatrix('Ftable.txt')
d = 2x3
400 1000 2500 70 100 150
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
and remove ".data" from your code. Avoid IMPORTDATA.
for i = 2:size(d,1) % do each row separately
rowdat = d(i,:);
rowdat = rowdat(~isnan(rowdat));
col = (rowdat(1:2:end-1));
val = rowdat(2:2:end);
nframes = col(end);
FBW(i-1,:) = zeros(1,nframes);
for j = 2:length(col)
if col(j) - col(j-1) >0
slope = (val(j) - val(j-1)) ./ (col(j) - col(j-1));
for k = 0:col(j)-col(j-1)
FBW(i-1, (col(j-1)+k)) = val((j-1)) + k*slope;
end
end
end
end
FBW
FBW = 1x70
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Taylor
Taylor 2024년 6월 18일
The problem is that "d" is not a structure so dot indexing does not apply.
  댓글 수: 2
BA
BA 2024년 6월 18일
@TaylorDo you have an idea what I should do?
Taylor
Taylor 2024년 6월 18일
Removing ".data" from the function should fix it (though I'm not entirely clear on the purpose of the function). There may be a better way to do what you're aiming for if you can clarify the goal of your function.

댓글을 달려면 로그인하십시오.

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by