How to use one matlab program to create a new .m file and insert executable code into it which is from a separate file?

Hello, I am attempting to create a function which determines variable values at different points in various separate programs which have certain variable values change. My program currently creates cell arrays of all code that is run in the program prior to each 'point'; these cell arrays look like this:
{0×0 char }
{'structure.A = 2; % Details' }
{'structure.B = 3; % Details' }
{'structure.C = 4; % Details' }
How would I go about copying this cell array into a new program and then running the program? This is what I have so far:
TempFileDirectory = [pwd,'\TempFile.m'];
FID = fopen(TempFileDirectory,'w');
fprintf(FID, '%s', char(FullText(1:65,:)));
fclose(FID);
TempFile;
This, however, does not work properly; instead of copying the code over into the new temporary file just as it is in the original file, it merges all of the code into one line which (obviously) cannot be executed. What do I do to fix this?
Thanks

 采纳的回答

I don't understand why you need to create files dynamically like this. I believe there must be some other ways to do this task efficiently. Regardless, it appears that you are using the fprintf() incorrectly. You also didn't mention the content of FullText variable. Look at the following example to get some idea about how can you create such a script file dynamically
x = {'a=1;'; 'b=2;'; 'c=3;'};
f = fopen('tmpfile.m', 'w');
fprintf(f, '%s\n', x{:});
fclose(f);
tmpfile;
As a side note, instead of creating file path by concatenation, as you are doing in TempFileDirectory, it is better to use fullfile() to join paths.

3 个评论

Ameer, thank you for your response.
Although there is certainly some way to complete this task more efficiently, I am a beginner to matlab and so don't know of other methods. FullText is a variable containing 65 lines consisting of the cell array written in my original question. My apologies for not making this clear. Thanks for your suggestion of using fullfile(), I will implement it in my program.
I am not entirely sure how you could convert what you have written in the first line of the code to something that I could utilize for a project which requires me to copy upwards of 100 lines as opposed to three. Do you have any ideas as to how I could apply the {'line';'line';'line'} method, which separates each line to a much larger and more automatic (I don't manually write the above for each line) program?
Thanks again, Ak
In your question, you already mentioned that you have a cell of code like this
{0×0 char }
{'structure.A = 2; % Details' }
{'structure.B = 3; % Details' }
{'structure.C = 4; % Details'
So I suppose that you already have a cell array containing all the code, and the only problem you are facing is that, the fprintf() command is pasting this cell array into one line in a new .m file. In the code I gave, you can use any cell array in place of x (not just limited to 3 elements). The code will create a separate line for each element of the cell array.
Ok, I see. My apologies, that makes a lot of sense. Thank you for your answer.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by