Need help, Why is Matlab not writing to file?

  • Context: MATLAB 
  • Thread starter Thread starter captain fink
  • Start date Start date
  • Tags Tags
    File Matlab Writing
Click For Summary
SUMMARY

The discussion addresses an issue with MATLAB not writing to a file due to improper variable type handling in the filename generation. The user attempted to create a filename using double variables 'f' and 'points' directly, which caused the fopen function to fail. The solution provided involves using the num2str function to convert these variables to strings, ensuring the filename is correctly formatted. Additionally, a correction is suggested for the fprintf function to include a missing format specifier.

PREREQUISITES
  • Familiarity with MATLAB programming language
  • Understanding of file I/O operations in MATLAB
  • Knowledge of data types and type conversion in MATLAB
  • Experience with formatting output using fprintf in MATLAB
NEXT STEPS
  • Learn about MATLAB file I/O functions, specifically fopen and fclose
  • Explore the num2str function for converting numeric values to strings in MATLAB
  • Study the use of format specifiers in the fprintf function for output formatting
  • Investigate error handling in MATLAB to manage file operation failures
USEFUL FOR

MATLAB users, data analysts, and engineers who need to perform file operations and troubleshoot issues related to data output in MATLAB.

captain fink
Messages
1
Reaction score
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
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 14 ·
Replies
14
Views
4K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
12K