How to remove a slash from fullfile?

조회 수: 11 (최근 30일)
Wesser
Wesser 2022년 9월 12일
댓글: Hiro Yoshino 2022년 9월 13일
I want the file path for wtf to read: 'C:\Users\jessi\Desktop\HydrusMC\Simulations\MC_1\Obs_Node.out'
but with the code as scripted below renders:
'C:\Users\jessi\Desktop\HydrusMC\Simulations\MC_\1\Obs_Node.out'
How do I drop the \ between the MC_ and the 1? I couldn't find any advise from other posts on this aspect of fullfile.
wtf= fullfile('C:\\','Users','jessi','Desktop','HydrusMC','Simulations','MC_',num2str(i),'Obs_Node.out');
  댓글 수: 1
Stephen23
Stephen23 2022년 9월 12일
This has nothing to do with FULLFILE, you can simply concatenate them together:
['MC_',num2str(i)]
or use a string:
"MC_"+i

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

답변 (2개)

Hiro Yoshino
Hiro Yoshino 2022년 9월 12일
How about this?
wtf= fullfile('C:\\','Users','jessi','Desktop','HydrusMC','Simulations',("MC_"+num2str(1)),'Obs_Node.out')
wtf = "C:\\/Users/jessi/Desktop/HydrusMC/Simulations/MC_1/Obs_Node.out"
  댓글 수: 2
Wesser
Wesser 2022년 9월 12일
Worked! Thanks!
If it's ok to pile on a follow up questions....
I am getting the error:
Error using fgets
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in fgetl (line 32)
[tline,lt] = fgets(fid);
Error in Concentration (line 14)
x=fgetl(Node_conc);
in the below code. I posed this question yesterday and thought that resolving the file path issue would make the error go away, but it didn't. Any suggestions?Malab doesn't seem to like me using fgetl.... but why?
num_sim=1000; % Number of Monte Carlo Simulations
Node_CONC=zeros(57759,num_sim); %preallocation for output from all monte carlos
path=cell(1,num_sim);
for i=1:num_sim
wtf= fullfile('C:\\','Users','jessi','Desktop','HydrusMC','Simulations',("MC_"+num2str(i)),'Obs_Node.out');
Node_conc = fopen('wtf','r'); % Open monte carlo output file in Path (i)
skip_lines=11; %skip all the lines until the actually output data
for k=1:(skip_lines)
x=fgetl(Node_conc);
end
temp = fscanf(Node_conc,'%f',[5,57759]); %scan the matrix of data
TEMP = temp'; % transpose data
temp_Conc = TEMP(:,5); % select the 5th column (concentration)
Node_CONC(:,i) = temp_Conc(:); % save the concentration a new file, one column per iteration of loop
fclose(Node_conc);
end
%Node_CONC(:,1)=temp(:,2)
Hiro Yoshino
Hiro Yoshino 2022년 9월 13일
wtf= fullfile('C:\\','Users','jessi','Desktop','HydrusMC','Simulations',("MC_"+num2str(i)),'Obs_Node.out');
Node_conc = fopen('wtf','r'); % Open monte carlo output file in Path (i)
You should pass the content of "wtf" to the function fopen.
In this case, you are passing the character array of 'wtf'.
You can tell the difference by checking out these as follows:
'wtf'
ans = 'wtf'
wtf= fullfile('C:\\','Users','jessi','Desktop','HydrusMC','Simulations',("MC_"+num2str(i)),'Obs_Node.out')
wtf = "C:\\/Users/jessi/Desktop/HydrusMC/Simulations/MC_0+1i/Obs_Node.out"

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


Voss
Voss 2022년 9월 12일
Combine the 'MC_' and the i into a single argument.
Like this:
wtf= fullfile('C:\','Users','jessi','Desktop','HydrusMC','Simulations',['MC_' num2str(i)],'Obs_Node.out');
Or this:
wtf= fullfile('C:\','Users','jessi','Desktop','HydrusMC','Simulations',sprintf('MC_%d',i),'Obs_Node.out');
  댓글 수: 1
Wesser
Wesser 2022년 9월 12일
Thank you. These worked as well as the method suggested above.

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

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by