save matrix command works sometimes and sometimes gives error
이전 댓글 표시
Hello,
I have been running a script that reads in data from multiple files, add them to a structure, and then save it. The code works just fine. But someitmes, it would give me an error.
Error using save
Unable to write file ColdFlow\Grid Resolution_ReOrder\MultitoSingleBlock\OutputFiles\OutputFilesForVisualization\Bfine.mat: Invalid argument.
Error in CollectData (line 110)
save(strcat(FolderName,'/',Case,'.mat'),'Structure','-v7.3')
If I rerun the script again, the error won't show up.
I do run this script on google drive, when I leave out multiple cases to run overnight, this haapens. I am not sure if this is a sync related issue, or matlab related issue. How do I fix it? I need a relaible code, than I can leave running for days to generate data.
댓글 수: 6
Walter Roberson
2023년 7월 22일
What is the length of the complete path, including the drive and including the folders above ColdFlow ?
At the moment it would not astonish me if you are exceeding the 247 character maximum path length.
Bruno Luong
2023년 7월 22일
"Unable to write file ColdFlow\Grid Resolution_ReOrder\MultitoSingleBlock\OutputFiles\OutputFilesForVisualization\Bfine.mat: Invalid argument."
Your folder starts with "ColdFlow" (relative path)? No drive, etc...
Bruno Luong
2023년 7월 22일
편집: Bruno Luong
2023년 7월 22일
where is your script is stored? I would put it in local, and not on google drive.
And maybe a try/catch with several attemps in case micro sync issue occurs.
Priyank Anilchandra Dhyani
2023년 7월 22일
Bruno Luong
2023년 7월 23일
편집: Bruno Luong
2023년 7월 23일
@Priyank Anilchandra Dhyani if I was you I would never use
addpath(strcat(pwd,'\',FolderName))
This is not robust as pwd can change, and the order of the path matters, multiple files with the same name can be found of different path,etc...
I'm not 100% sure if you have to add the absolute path of the file or the rootpath where the relative path is based on in the search path (meaning pwd).
More importantly I'm not sure how MATLAB search path would behave if for some reason the Google drive losts sync and strcat(pwd,'\',FolderName) become temporary invalid.
If you want to write robust code, this is NOT the way you should do.
I would reduce as much as possible program, context, config, etc.. that depend on google drive, mainly the only thing that remain is the file that you try to read and write.
Priyank Anilchandra Dhyani
2023년 7월 23일
답변 (1개)
Jan
2023년 7월 22일
0 개 추천
Avoid strcat to create a folder name, but use fullfile, which cares for the correct separators.
A try/catch block is the way to go to catch and handle errors.
댓글 수: 3
Priyank Anilchandra Dhyani
2023년 7월 22일
Jan
2023년 7월 25일
"Is there a fucntion similar to strcat?" - I've suggested fullfile already.
Instead of changing the path, it is safer to work with absolute path names. Replace:
restoredefaultpath
addpath(pwd)
cd ..
addpath(strcat(pwd,'\',FolderName))
by
FullFolder = fullfile(pwd, FolderName);
if isfile(fullfile(FullFolder, 'ReyAvg.tec'))
...
end
Priyank Anilchandra Dhyani
2023년 7월 27일
카테고리
도움말 센터 및 File Exchange에서 Software Development에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!