Why is trailing back space removed from my folder name?

1 view (last 30 days)
I have a subfolder named .\results (I give the whole name)
Allowing the user to change it I have name app.defOutLoc as default and the app.OutLoc once confirmed.
app.OutLoc = app.defOutLoc;
K>> defOutLoc
defOutLoc =
"C:\Users\CPSLab\Documents\OdorChoiceDev\results\"
K>> app.OutLoc
ans =
"C:\Users\CPSLab\Documents\OdorChoiceDev\results"
So when I add the folder name to file name I end up saving \resultsMyFileName.txt in the parent folder
Who asked you to trim my \? and what do I do now?
I've added at some points
% Don't forget the ending \
app.OutLoc = append(app.OutLoc,"\");
but now need to chase down every time I do an assignment and add it back!
You would think it could at least be done right here where I build the name:
app.StartSessionTime = ...
char(datetime('now','Format',"_yyMMdd_HHmmss"));
baseFileName = append(app.MouseName.Value, ...
app.StartSessionTime);
app.SessionFileName = append(app.OutLoc, ...
"MC_",baseFileName,"_session.m");
app.SessionFile = fopen(app.SessionFileName,'at'); % allow appending
I suppose there is some special way to combine file parts instead of append where ML put it back that I missed.
OK I found fullfile() Now to get it to properly put all the pieces together. Like this?
app.SessionFileName = fullfile(app.OutLoc, append("MC_",baseFileName,"_session"),"m";
  1 Comment
Gavin
Gavin on 28 Jan 2025
nope. the .m is part of the file name. I guess extensions aren't universal. Not that I need it but I suppose the idea is to let coe run on Mac or linux where the use the original / instead of the Microsoft abomination of switching it to \
app.SessionFileName = fullfile(app.OutLoc, ...
append("MC_",baseFileName,"_session.m"));
Done. Leaving it here in case anyone else has this problem.

Sign in to comment.

Answers (1)

Voss
Voss on 28 Jan 2025
I don't know why the trailing backslash is missing from app.OutLoc. Maybe app.defOutLoc is not the same as defOutLoc.
Anyway, you should use fullfile to construct file paths.
app.OutLoc = "C:\Users\CPSLab\Documents\OdorChoiceDev\results"; % missing trailing \
app.MouseName.Value = 'whatever';
app.StartSessionTime = ...
char(datetime('now','Format',"_yyMMdd_HHmmss"));
baseFileName = append(app.MouseName.Value, ...
app.StartSessionTime);
app.SessionFileName = fullfile(app.OutLoc, ...
append("MC_",baseFileName,"_session.m"));
disp(app.SessionFileName) % fullfile() puts a / because we're on Linux here
C:\Users\CPSLab\Documents\OdorChoiceDev\results/MC_whatever_250128_205544_session.m

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!