Why does the same code that runs on HDD doesn't run on SSD?

I pass data between Matlab and another software by writing it in a file. I have a loop in the code that very much looks like this:
for i = 1:100
fid = fopen('test.dat','w');
fprintf(fid,'#Data Generated %s \n\n',datestr(now));
fclose(fid);
delete('test.dat');
end
I have both HDD and SSD on my computer. if I run the code from SSD, it runs into trouble somewhere in the loop (a different iteration each time). It seems that it cannot generate the file id (returns -1). If I copy the exact same file on the HDD, it runs fine. My guess is that for some reason Matlab is going faster than the OS (Windows 10) -- trying to create a file with the same name before the OS is done deleting it. If i use a while loop around fopen command, it runs fine, but that doesn't seem efficient and it would make things more complex, especially in my main code that is much messier.
Since running the code on SSD is much faster, I would prefer that. Any ideas how to solve the issue?
Thanks!

2 Comments

Is it necessary to use delete inside the loop? Because fopen will overwrite the file if you don't tell it otherwise.
the reason I use delete is to showcase the problem with the OS. In my main code, another program is called after fclose, from inside matlab. that would hand over the control to the OS. when that program is done, its output is written in a file that matlab has to open and read its content, using fopen command. It runs into exact same problem. Initially I thought the problem is coming from "system" command or "!" that I had used to run the other software from inside matlab. but it turned out that any command that gets the OS involved, like delete in the above example, runs into the same issue. It seems that the OS hands the control back to Matlab before it is done with finishing what it is supposed to do (delete in the above example, or writing the output file of another program that is called by matlab in my main application).

Sign in to comment.

Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 18 Jan 2018

Commented:

on 18 Jan 2018

Community Treasure Hunt

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

Start Hunting!