MATLAB Practice questions solution

Hello all,
I am new to MATLAB and I am still trying to learn it by myself. I was trying to solve the questions posted by MATLAB. I know I didn't use the double function as suggested so please enlighten me with your ideas and let me know if i was at least close to what the problem is asking for:
The provided text file (readings.txt) contains a timestamp broken up into year, month, day, hour, minute, second, and timezone components, as well as a reading from a sensor. Write a script that reads the data from the file using the textscan function.
The script must:
  • Convert the timestamps into single numeric serial date numbers stored in a variable named dates
  • Ignore the timezone component of the timestamp by not reading it into the workspace
  • Place numeric values for the readings in a single array of type double with a variable name of readings
clc
clear all
fileID1 = fopen('readings.txt');
dates = textscan(fileID1, '%s %s %s %s %s %s %s %s');
fclose(fileID1);
whos dates;
celldisp(dates)
fileID2=fopen('readings.txt')
C=textscan(fileID2, '%q %q %q %q %q %q %*q %q');
fclose(fileID2);
whos C;
celldisp(C)
fileID3=fopen('readings.txt');
D=textscan(fileID3,'%*q %*q %*q %*q %*q %*q %*q %q');
fclose(fileID3);
whos D;
celldisp(D)

Respuestas (3)

Image Analyst
Image Analyst el 3 de En. de 2021

0 votos

Perhaps datenum()?
>> d=datenum(now)
d =
738159.473416505

7 comentarios

Ali Awada
Ali Awada el 4 de En. de 2021
How does that answer the problem. please elaborate
Image Analyst
Image Analyst el 4 de En. de 2021
It turns complicated date formats into a floating point number, which is easier to sort. So it does this:
  • Convert the timestamps into single numeric serial date numbers stored in a variable named dates
If you want more specific help, then attach the 'readings.txt' file which you forgot to attach. Because otherwise we really don't know what format your "timestamps" are in. There is a dizzying variety of time and date formats and functions. It can get really confusing.
Ali Awada
Ali Awada el 5 de En. de 2021
The readings file is in the link i posted. Here it is for ease of access
You should have tried datenum like I suggested. I did and it seems to work fine:
t = readtable('readings.txt')
d = datenum(t.Year, t.Month, t.Day, t.Hour, t.Minute, t.Second)
readings = t.Reading
plot(d, readings, 'b.-', 'LineWidth', 2, 'MarkerSize', 30);
grid on;
title('Alis Homework', 'FontSize', 20);
xlabel('Date/Time Stamp', 'FontSize', 20);
ylabel('Reading', 'FontSize', 20);
Ali Awada
Ali Awada el 10 de En. de 2021
Hey. Thanks for a fast reply. But the MATTLAB practice test does not ask to graph the stored value (please see above problem description) . I think you misubderstood what the problem is asking for but thanks for helping anyway.
Image Analyst
Image Analyst el 10 de En. de 2021
Then just use the first 3 lines and skip the rest of the lines that have to do with plotting.
Ali Awada
Ali Awada el 11 de En. de 2021
the problem asks us to use the function textscan

Iniciar sesión para comentar.

Kazem Gheysari
Kazem Gheysari el 24 de Ag. de 2021
Editada: Kazem Gheysari el 24 de Ag. de 2021
clear
fileID = fopen('readings.txt');
C_text = textscan(fileID,'%s',8,'Delimiter',' ');
C = textscan(fileID,'%f %f %f %f %f %f EST %f','Delimiter',' ','EmptyValue',-Inf);
fclose(fileID);
Y = C{1,1};
M = C{1,2};
D = C{1,3};
H = C{1,4};
MN= C{1,5};
S = C{1,6};
dates = datenum(Y,M,D,H,MN,S)
readings = C{1,7};

1 comentario

Image Analyst
Image Analyst el 24 de Ag. de 2021
What is matlab1.com? (I did not click your link because there is no explanation)

Iniciar sesión para comentar.

Abhiram Rayidi
Abhiram Rayidi el 27 de Jun. de 2022
syms y(t);
dsolve(diff(y)==y, y(0)==1, 'Expansion Point', 0)
Error using symengine
Unexpected 'identifier'.

Error in mupadengine/evalin_internal

Error in dsolve>mupadDsolve (line 333)
sys = [sys_sym reshape(evalin_internal(symengine, sys_str), 1, [])];

Error in dsolve (line 203)
sol = mupadDsolve(args, options);

Categorías

Preguntada:

el 3 de En. de 2021

Comentada:

el 20 de En. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by