How do i get a function to open any text file on a computer

조회 수: 7 (최근 30일)
Juan Palacios
Juan Palacios 2019년 5월 23일
편집: Juan Palacios 2019년 5월 23일
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
Juan Palacios
Juan Palacios 2019년 5월 23일
Doesnt work, not enough imput arguments for the fuction.
Jan
Jan 2019년 5월 23일
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')

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2019년 5월 23일
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
Juan Palacios
Juan Palacios 2019년 5월 23일
편집: Juan Palacios 2019년 5월 23일
Is that for the GUI intergration or to run any file? That code would not work as i need it to ananlyse the data on each line of the file and scatter plot each entry being updated. The data is in a text file.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by