Problem with saving file with built in new line

To get the username from the system, I'm using the follow syntax:
if ispc()
[~,username] = system('echo %username%'); %On Windows
else
[~,username] = system('echo $USER'); %On MAC
end
This will return the username as a string. But there will be a newline character at the end of the username, how can I get the username without the newline character at the end? I'm also using the built-in date() function.
function saveTable(A,B,C,D)
mat1 = [A, B, C];
if ispc
[~,username] = system('echo %username%'); %On Windows
else
[~,username] = system('echo $USER'); %On Mac
end
str1 = fprintf('%s',D);
str2 = fprintf('%s',username);
str3 = date;
filename1=fprintf('%s_%s_%s',str1,str2,str3,'s');
save filename1.dat mat1 -ascii
end

Answers (1)

I generally just do the expedient for such stuff --
username=username(username>=' ');
You can be more elegant (for some definition of "elegant" :) ) with regexp or the like but the above deals with whichever is the actual \n sequence, one or two characters.

1 Comment

I'm a complete noobie, could you show me how to use your code line in context of my function?

This question is closed.

Asked:

on 1 Mar 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!