How to write to a file from anywhere?

I am creating a program that checks simulink models for errors and log those errors in an excel spreadsheet. This sheet is stored in the script folders, and multiple functions write to it. Right now the program will break when I am not currently in the scripts folder, but I want to know if there is a way for me to not be in the script folder while the program is running and it still works?
function eraseWhiteSpace(app)
%%%%%%App Specific Stuff
% stop the timer while we are running
stop(app.projectTimer);
% disable the generate button while we are running
%app.RunCleanupButton.Enable = 'off';
app.StatusArea.Value = 'Running erase whitespace function. This can take awhile...';
filename = app.TopModelEditField.Value;
[folder, system, extension] = fileparts(filename);
load_system(system);
%%%%%%End App Specific Stuff
%Remove Old whitespace findings mat file
projects = slproject.getCurrentProjects;
projectRoot = projects.RootFolder;
saveFolder = [projectRoot '\scripts'];
saveFileFullName = [saveFolder '\whiteSpaceFindings.mat'];
warning('off','all')
baseFileName = 'ErrorReport.xlsx';
fullFileName = fullfile(saveFolder, baseFileName);
xlswrite(fullFileName ,baseFileName,'ErrorReport');
warning('on','all')
R = table(Diagram,portsWithBlanks,'VariableNames',varNames);
writetable(R,baseFileName,'Sheet','PortsWithBlanks');

4 Comments

Deon - is the problem that
projectRoot = projects.RootFolder;
saveFolder = [projectRoot '\scripts'];
saveFolder is invalid? That there isn't a scripts folder in the project root?
No, there is a script folder and there is not invalid error. The only error I am getting is when I am not in the script folder and the program can not write to the Error report, becuase the error report is only in the script folder(where it should be). I am trying to see if it is possible to be in another folder while the program is running and have it write the error report even though I am in a separate folder.
You could try using "[fPath '\' fName]" to specify where to save your excel file where:
[fPath, fName, fExt] = fileparts(fullfile(path,file));
It's not saving that is my issue. I can save it and run it just fine. What my leader wants me to do is see if I can write to file from anywhere.
For example the excel file is in the scripts folder, but I am currently in the projects folder. Right now I would get an error because it can't write to the excel file since it is in another folder, which makes sense to me. I want to know if it is possible to have it write to that file even though I'm in a completely different folder.

Answers (0)

This question is closed.

Asked:

on 27 Apr 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!