What is wrong with my code when it has no errors but does nothing when i try to run it?

10 visualizzazioni (ultimi 30 giorni)
I am not sure how to troubleshoot the cause of this issue. I am trying to calculate something from the data in specific columns of several different CSV files at once so I can efficiently average the data. This is the first time I have used matlab for such purposes so I apologize if I am missing something relatively obvious. I am also doing this on a macbook pro if that is a possible problem.
Here is my code:
strDir = '/Users/Pathway/Documents/csvfiles';
fnames = dir(strcat(strDir, '*.csv'));
numfids = length(fnames);
m = zeros(1,numfids);
u = zeros(1,numfids);
for i = 1:numfids
f = fopen(strcat(strDir, fnames(i).name));
X = textscan(fid, '%s%f%f%f%f%f%f%f%f', 'Delimiter', ',', 'HeaderLines', 47);
col1 = X(:,3);
col2 = X(:,4);
m(i) = (X(70,4) - X(20,4))./(X(70,3) - X(20,3));
D = 20; % M/L
V_d = 100; % Constant Drain Voltage
C_i = 1.5E-8; % Capacitance of Dielectric F/ cm^2
u(i) = (m(i))./(D .* C_i .* V_d); % mobility
fclose(f);
end
disp(m)
disp(u)
Even though I write in for the values to be displayed nothing comes up even though there are no errors. Its as if no files are found but I can assure you that the file being directed to(csvfiles) exclusively has 7 cvs files within it. Please take a look at the attached screen shot to see what the workspace looks like after running the code.
Does anyone have any advice? Any form of assistance will be appreciated. Thank you.

Risposta accettata

Stephen23
Stephen23 il 10 Apr 2017
Modificato: Stephen23 il 10 Apr 2017
Why does it not work? Because no files are found.
Why are no files found? Because you concatenate using strcat instead of fullfile.
What is the difference? Try it yourself and see:
>> strcat('mypath\somedir','filename.txt')
ans = mypath\somedirfilename.txt
>> fullfile('mypath\somedir','filename.txt')
ans = mypath\somedir\filename.txt
So when you use strcat the filename does not get separated from the file path by a file separator character. Ergo you provide an non-existent file path, and MATLAB cannot find any files with those names.
"Its as if no files are found but I can assure you that the file being directed to(csvfiles) exclusively has 7 cvs files within it"
"I am not sure how to troubleshoot the cause of this issue"
When you learn to debug code the most important thing is to change your perspective. It does not matter that there are seven files in that folder, or that you think (want/wish/believe/assure/...) that the code should work with them. Totally irrelevant. Clearly it does not do what you want, therefore there is a reason/bug (or more), and therefore you can find out what the bugs are. Debugging means actually looking at what the code is really doing (not what you imagine it is doing, or what you assure us it is doing). If you had started to check your own code then the second line of code would have given you a major help:
size(fnames)
or by simply displaying fname in the command window or looking at it in the Variable Viewer. All of these are trivially easy things to do. You can do them too. Then it is easy to ask "why is fnames empty?" Well, lets have a look at the path string:
strcat(strDir, '*.csv')
And what does it show you?
PS: A typical beginner's mistake is to write pages of code, not test or check any of it, and then be totally unable to figure out where all of the bugs are. Write one line, read the function help to understand how they work, check the line output by hand, and then test it by running it. Make sure that the line does what you need it to do. Then move on to the next line.

Più risposte (0)

Categorie

Scopri di più su Variables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by