How can I change in the directory while using dlmwrite?

7 views (last 30 days)
Hi
I have a problem, I use dlmwrite to write data in a excel file, but when I run the code, I have noticed that the directory in which lake_para_file is addressed does not exist at al.
lake_par_file = tempname;
fid=fopen(lake_par_file,'wt');
fprintf(fid,'\n\n');
dlmwrite(lake_par_file, [[1:length(K_lake)]',data_lake{2},data_lake{3},data_lake{4},(1:length(K_lake))'],'delimiter','\t','-append'); % 1:length(K_lake) is the length of the parameter file.
fclose(fid);
Does anybody know how can I change the directory?
  3 Comments
Walter Roberson
Walter Roberson on 11 Mar 2022
I am not clear on what you mean about the directory. Are you saying that if you
td = fileparts(lake_par_file)
isfolder(td)
will say false (0)?
Sahar khalili
Sahar khalili on 11 Mar 2022
no it says true. But I cannot find such directory that it is addressed in the workspace.

Sign in to comment.

Answers (1)

Jan
Jan on 11 Mar 2022
Edited: Jan on 11 Mar 2022
I do not understand, what the problem is. The function tempname creates a file in your TEMP folder. Under Windows this is: C:\Users\<YourAccount>\AppData\Local\Temp\ . The file name is chosen randomly, e.g. 'tp3ed79371_4d80_4861_aa70_9b2bf68b293c' without a file extension.
What does "addressed in the workspace" mean?
Why do you open the file in addition by:
fid=fopen(lake_par_file,'wt');
"Does anybody know how can I change the directory?" - which one? The cd command changes the current directory, but this is a fragile method to determine the output folder. Prefer to use an absolute file name instead, e.g.
Folder = cd; % Or 'C:\Temp' or which folder you prefer
File = 'myData.csv';
dlmwrite(fullfile(Folder, File), ...
[(1:length(K_lake)).', data_lake{2:4, (1:length(K_lake)).'], ...
'delimiter', '\t', '-append');

Categories

Find more on Search Path in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!