How do i get a function to open any text file on a computer
Mostrar comentarios más antiguos
Hi,
I have written the code below and i was wondering how i could get the function to open any txt file on the pc not just the one i assigned. So if i implemet a GUI to call up this fuction for any data it will read it?
function Sensor_data();
fid = fopen ('Gradual opening of tap.txt');
initial_line = fgetl(fid);
%Variables for the data (Garvity, Volts of the pump, and the Density of
%water)
data_figure();
current_time = 0;
gravity = 9.81;
vs = 5;
density = 997;
%While loop for for working out the data from each line of data and
%ploting the data.
while ischar(initial_line)
data = sscanf(initial_line,'%f V, %d counts, %d ms');
%extarctting the data from the file and unpacking it by line.
v = data(1); %volts
pulses = data(2); %amount of plulse
t = data(3); %time intervals
% calculating the remainder of the data to be plotted
current_time = current_time + t;
P = ((v/vs)-0.04)/0.0018;
h = P*1000/(density*gravity);
Q = (1000*pulses)/(330*current_time);
hydraulic = (Q*P)/1000;
plot_data(hydraulic,current_time,h,P,Q)
initial_line = fgetl(fid);
end
% Function for plotting figures for each individual graph
function data_figure()
figure(1)
clf
%Pressure (P) v Time(current_time) data plot
subplot(2,2,1)
hold on
grid on
title ('Pressure v Time')
xlabel('Time(ms)')
ylabel('Pressure (Pa)')
%Flow rate (Q) v Time (current_time) data plot
subplot(2,2,2)
hold on
grid on
title ('Flow rate v Time')
xlabel('Time(ms)')
ylabel('Flow (L/ms')
%Flow rate (Q) v Head pressure (h)
subplot(2,2,3)
hold on
grid on
title ('Flow Rate v Head Pressure ')
xlabel('Flow (L/ms)')
ylabel('Pressure (Pa)')
%Hydraulic Power (hydraulic) v Flow rate (Q) data plot
subplot(2,2,4)
hold on
grid on
title ('Hydraulic Power v Flow Rate')
xlabel('Power(W)')
ylabel('Flow (L/ms)')
end
function plot_data(hydraulic,current_time,h,P,Q)
subplot(2,2,1);
scatter(current_time,P)
subplot(2,2,2);
scatter(current_time,Q)
subplot(2,2,3);
scatter(Q,h)
subplot(2,2,4);
scatter(Q,hydraulic)
drawnow;
end
end
3 comentarios
Why not just supply the filename/path as an input argument? I.e. instead of this:
function Sensor_data();
fid = fopen ('Gradual opening of tap.txt');
simply do this:
function Sensor_data(file);
fid = fopen(file);
And then just call your function with whatever filename/path you want.
Although beginners like sticking UI controls inside functions, I would recommend avoiding doing that if you are interested in writing clear, efficient, testable code.
Important: Note that you should fclose every file that you fopen. Accumulating handles to open files can slow down MATLAB and can lead to it crashing.
Juan Palacios
el 23 de Mayo de 2019
Jan
el 23 de Mayo de 2019
How do you start the function? By pressing the "Run" button in the editor? If you define the function with an input argument, you have to call it with an input argument also:
Sensor_data('YourTextFile.txt')
Respuestas (1)
Jan
el 23 de Mayo de 2019
function Sensor_data()
[fileName, filePath] = uigetfile('Choose some Sensor data:');
if isequal(fileName, -1)
disp('User aborted file choosing');
return;
end
file = fullfile(filePath, fileName);
... Your code
1 comentario
Juan Palacios
el 23 de Mayo de 2019
Editada: Juan Palacios
el 23 de Mayo de 2019
Categorías
Más información sobre Graphics Object Properties en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!