Undefined function or variable in a POD matlab code

Hello all,
I am trying to run the following code:
function VelocityDistributionPOD (SnapshotsAddress)
%Method of Snapshots
%Section 1 -­‐ Input snapshots
%Each snapshot (txt file) contains four columns. The first two columns are the velocity
%distribution grid point coordinates for x and y direction, respectively. The last two columns
%are u and v velocities, respectively.
SnapshotsAddress = pwd;
files = dir([SnapshotsAddress,'*.txt']);
n_snapshots = size(files,1);
for j=1:n_snapshots
fid = fopen([SnapshotsAddress,files(j).name], 'r');
data = fscanf(fid,'%f %f %f %f',[4,inf]);
x = data(1,:); % x coordinate
y = data(2,:); % y coordinate
U(j,:) = data(3,:); % u velocity
V(j,:) = data(4,:); % v velocity
fclose(fid);
end
%Section 2 -­‐ Compute spatial correlation matrix C
c1 = U*U';
c2 = V*V';
C = (c1+c2)/n_snapshots;
%Section 3 -­‐ Solve the eigenvalue problem: C * EigenVector = EigenValue * EigenVector
[beta, lmd] = svd(C);
%Section 4 -­‐ Calculate basis functions
phix = U'*beta;
phiy = V'*beta;
% Normalize basis functions
GridNum = size(x,2);
for j=1:n_snapshots
PhiNor = 0;
for i=1:GridNum
PhiNor = PhiNor + phix(i,j)^2 + phiy(i,j)^2;
end
PhiNor = sqrt(PhiNor);
phix(:,j)= phix(:,j)/PhiNor;
phiy(:,j)= phiy(:,j)/PhiNor;
end
%Section 5 -­‐ Calculate coefficient
TimCoeU = U*phix;
TimCoeV = V*phiy;
TimCoe = TimCoeU + TimCoeV;
%Section 6 -­‐ Export basis functions
for a=1:n_snapshots
FilNamPhi = 1000+a;
PhiOut = fopen([SnapshotsAddress,num2str(FilNamPhi),'.txt']', 'wt');
fprintf(PhiOut, '#DaVis 7.2.2 2D-­‐vector 16 145 145 "position" "mm" "position" "mm" "velocity" "m/s"\n');
phia = [x;y;phix(:,a)';phiy(:,a)'];
fprintf(PhiOut, '%20.9f %20.9f %20.9f %20.9f\n',phia);
fclose(PhiOut);
end
% Write coefficients into excel file
xlswrite([SnapshotsAddress,'TimCoe.xlsx'],TimCoe);
I have 3 .txt files in the same directory but it seems whenever I try to run I get the following error:
Undefined function or variable 'U'.
Error in VelocityDistributionPOD (line 20)
c1 = U*U';
I have tried to change the way to define the directory and reading the .txt files, couldn't get it to work.
Any ideas?, thank you!
Code reference:
Chen, H., Reuss, D. L., Hung, D. L., & Sick, V. (2013). A practical guide for using proper orthogonal decomposition in engine research. International Journal of Engine Research, 14(4), 307-319.

 採用された回答

Jan
Jan 2022 年 12 月 22 日
移動済み: Jan 2022 年 12 月 22 日

0 投票

A strange detail:
function VelocityDistributionPOD (SnapshotsAddress)
SnapshotsAddress = pwd;
The folder name provided as input is overwritten. Why?
If the current folder does not contain txt files, U is not created. Then files = dir([SnapshotsAddress,'*.txt']); is empty and the loop with the import is not entered.
Remove this line
SnapshotsAddress = pwd;
and provide the correct folder as input. Then consider folder with and without trailing file separator:
files = dir(fullfile(SnapshotsAddress, '*.txt'));

4 件のコメント

Hussein Kokash
Hussein Kokash 2022 年 12 月 22 日
Hey ther Jan, thanks for your valuable input, I made those changes but still getting this:
Error using fullfile (line 67)
All inputs must be strings, character vectors, or cell arrays of character vectors.
Error in VelocityDistributionPOD (line 7)
files = dir(fullfile(SnapshotsAddress, '*.txt'));
Any way to get around this? appreciate it
Hussein Kokash
Hussein Kokash 2022 年 12 月 22 日
txt files are also within the directory!
Jan
Jan 2022 年 12 月 23 日
編集済み: Jan 2022 年 12 月 23 日
What is the contents of the variable SnapshotsAddress? How do you provide the name of the folder, if it is neither a string nor a char vector?
"txt files are also within the directory!" - Matlab's error message tells clearly, that there are no txt files in the current folder.
Hussein Kokash
Hussein Kokash 2022 年 12 月 23 日
You are right Jan, it seems that they have not included what are the contents of the variable SnapshotsAddress. I will look further into it.
Thank you for your time.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

製品

リリース

R2018b

質問済み:

2022 年 12 月 22 日

コメント済み:

2022 年 12 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by