How do i get a function to open any text file on a computer
13 views (last 30 days)
Show older comments
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 Comments
Jan
on 23 May 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')
Answers (1)
Jan
on 23 May 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
See Also
Categories
Find more on MATLAB Report Generator in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!