%%%%%%%%%%%%%% - 旧%%%%%%%%%%%%%%%%%%%%%%<%
我通过提高效率来修改代码。我在代码中有新的错误,而不是访问单元数组的专家
for i = first:N_Files
mat{i} = load(files(i).name);
A{i} = mat{1,i};
x{i} = A{:,i};
ind{i} = find(x{i}>= -0.5 & x{i}<=0.5);
% New error
B{i} = A{ind{i,:},:}; **Index exceeds matrix dimensions.**
xx{i} = B(:,1);
end
%%%%%%%%%%%%% 1 - 旧%%%%%%%%%%%%%%%%%%%%%%<<
我写了一个例程来访问放在不同文件夹中的文件。 然后构建一个单元数组,其中存储文件数据,并使用例程:
for i = first:N_Files
mat{i} = load(files(i).name); % 1x3 cell
eval(['A' num2str(i) '= mat{1,i} ;']) % A1,A2,A3 dim : 114336x6 double
end
A = {A1, A2, A3}; cell array 1x3
我有一个访问在评估矩阵单元格时创建的向量的问题。
for i = first:N_Files
eval(['x' num2str(i) '= A{:,i} ;']) % create x1, x2, x3
%错误代码
eval([&#39; ind&#39; num2str(i)&#39; = find(&#39; x&#39; num2str(i)&gt; = -0.5&amp;&#39; x&# 39; num2str(i) &lt; = 0.5);&#39;])
end
%需要此索引解决方案
ind1 = find(x1>= -0.5 & x1<=0.5);
ind2 = find(x2>= -0.5 & x2<=0.5);
ind3 = find(x3>= -0.5 & x3<=0.5);
我必须使用find函数获得eval函数, 它有可能吗? 有一个有用的解决方案 感谢
%%%%%%%%%%%%%完成代码%%%%%%
这是我必须得到的代码。
for i = first:N_Files
A{i} = load(files(i).name); % 1x3 cell
x = A{:,1};
ind{i} = find(x(:,1)>= -0.5 & x(:,1)<=0.5); % This looks cleaner
B{i} = A{i}(ind{i},:); % Correct,first access element i in A then filter.
xx{i} = B{i}(:,1); %1x3 cell **what i really wanted**
yy{i} = B{i}(:,2);
zz{i} = B{i}(:,3);
u{i} = B{i}(:,4); % vettore velocità
v{i} = B{i}(:,5);
w{i} = B{i}(:,6);
c1{i} = [-0.5:0.01:0.5];
c2{i} = [0:0.01:2] ;
[X{i},Z{i}] = meshgrid(c1{i},c2{i});
U{i} = griddata(xx{i},zz{i},u{i},X{i},Z{i});
U{i}(isnan(U{i}))=0; % interpolazione ai bordi
W{i} = griddata(xx{i},zz{i},w{i},X{i},Z{i});
W{i}(isnan(W{i}))=0;
figure
pcolor(X{i},Z{i},(U{i}.^2+W{i}.^2).^0.5);
shading(gca,'interp')
title('Velocità')
colorbar;
axis square
hh = streamslice(X{i},Z{i},U{i},W{i});
set(hh,'color','k');
end
答案 0 :(得分:1)
好的,我会尽力帮助你。首先,如果您的单元格的维度为1xN,则只需要一个索引:A{i} = mat{i}
。但是,您会看到A==mat
,这意味着两者都不需要。在下一行,您执行x{i} = A{:,i}
,这与您在前一行上的操作相同=&gt; x==mat
。这意味着我们可以删除两行。然后我猜你的目标是找到每个文件的绝对值小于0.5的索引,并将每个文件存储在一个单元格中,对吧?然后是错误:通过执行A{ind{i,:},:}
,您实际上是在引用A
本身,而不是A
的每个元素。 A
的大小为1 * nFiles。你要做的确实如此:B{i} = A{i}(ind{i})
。
所以,如果这不是你想要的,请评论。否则,删除冗余变量。 并确保不要将细胞与基质混合。您使用单元格作为数组的容器:a {n}引用单元格元素n中的数组,而{n}(m)引用位于单元格元素n中的数组中的矩阵元素。祝你好运!
for i = first:N_Files
mat{i} = load(files(i).name);
%A{i} = mat{1,i}; % not needed mat==x
%x{i} = A{:,i}; % not needed x==A
x = mat; % Fix this later, I do not want to change any variable names.
% ind{i} = find(x{i}>= -0.5 & x{i}<=0.5);
ind{i} = find(abs(x{i})<=0.5); % This looks cleaner
%B{i} = A{ind{i,:},:}; **Index exceeds matrix dimensions.**
B{i} = A{i}(ind{i}); % Correct,first access element i in A then filter.
xx{i} = B(:,1); %I do not know what you tries to do but probably
%xx{i}=B{i} => x==B so one variable is redundant.
end