Need help, Why is Matlab not writing to file?

In summary: Should be:fprintf(Mo, '%.2f\t%.2f',y,M);In summary, the conversation is discussing a program that calculates and outputs the moment of a force applied to different points. It prompts the user for the force, closest and farthest points from the pivot, and the number of points to calculate. It then generates a file with the given information and calculates the moment at each point. However, there may be errors with the file name and formatting of the output.
  • #1
captain fink
1
0
clc
clear

f = input('Enter the force applied in lbs: ');
p_c = input('Enter point closest to pivot: ');
p_f = input('Enter point farthest from pivot: ');
filename = input('name of file is: ','s');
points = input(' # of points to calculate: ');

x = [p_c;rand(points,1)*p_f-p_c+p_c;p_f];
y = sort(x);
pos = find(y<p_c);
pos1 = find(y>p_f);
y(pos) = [];
y(pos1) = [];

%disp(y) points

M = y*f;

%disp(M) moment

final = [y,M];

Mo = fopen([filename,'_',f,'_',points,'.txt'], 'w');

%fid = fopen(string, option)

%writing

fprintf(Mo, 'ID:\t%s\nF:\t%2.2f\n point closest:\t%2.3f\n point farthest:\t%2.3f...\n',filename,f,p_c,p_f);
fprintf(Mo, ' points\t moment\n');
fprintf(Mo, '%.2f\t.2f',y,M);
fclose(Mo);

Your thoughts would be helpful because I don't see why this isn't working
 
Physics news on Phys.org
  • #2
The variables f, and points (which are of type double), are used to generate you file name without being converted to string.
captain fink said:
Mo = fopen([filename,'_',f,'_',points,'.txt'], 'w');
Try, using num2str function, for example
Code:
Mo = fopen([filename,'_',num2str(f),'_',num2str(points),'.txt'], 'w');

Also, you might want to have a closer look at:
captain fink said:
fprintf(Mo, '%.2f\t.2f',y,M);
think, it's missing a % after \t.
 

1. Why is my Matlab code not writing to a file?

There could be several reasons why your Matlab code is not writing to a file. Some common issues include incorrect file path, insufficient permissions, or an error in your code. Make sure to check your code for any errors and ensure that the file path is correct and that you have the necessary permissions to write to the file.

2. How do I troubleshoot Matlab not writing to a file?

To troubleshoot this issue, you can start by checking for any errors in your code. Make sure that you have specified the correct file path and that you have the necessary permissions to write to the file. You can also try using the "diary" command to log any errors that occur during the writing process.

3. Why am I getting an error message when trying to write to a file in Matlab?

If you are getting an error message when trying to write to a file in Matlab, it could be due to several reasons. Some common causes include incorrect file path, insufficient permissions, or an error in your code. Check your code for any errors and make sure that the file path is correct and that you have the necessary permissions to write to the file.

4. How can I save my data in Matlab to a file?

To save data in Matlab to a file, you can use the "save" command followed by the file name and the data you want to save. Make sure to specify the correct file path and check for any errors in your code. You can also use the "dlmwrite" command to save data to a text file.

5. Can I write to a file in Matlab without overwriting existing data?

Yes, you can write to a file in Matlab without overwriting existing data by using the "fprintf" command. This command allows you to append new data to the end of an existing file without overwriting the existing content. Make sure to specify the correct file path and check for any errors in your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
997
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top