Javascript reading files: how should I have asked?

Click For Summary
SUMMARY

The discussion centers on the challenges of reading local data files in JavaScript, particularly when running code in a web browser. The user successfully created a workaround using MATLAB to generate a JavaScript function that outputs a data array, which is then called in an HTML file. Key insights include the limitations of JavaScript in a browser sandbox and the advantages of using Node.js for server-side file access. The user reflects on how to better frame their original question to receive more relevant assistance.

PREREQUISITES
  • Understanding of JavaScript and its execution environment in web browsers
  • Familiarity with MATLAB for data processing
  • Knowledge of Node.js for server-side JavaScript execution
  • Basic concepts of JSON for data interchange
NEXT STEPS
  • Learn how to use Node.js to read and write files in JavaScript
  • Explore the XMLHttpRequest API and its limitations with local files
  • Investigate Three.js for 3D rendering and how to load data into it
  • Study best practices for formulating programming questions to receive effective responses
USEFUL FOR

Web developers, data scientists using MATLAB, and anyone interested in integrating JavaScript with local data files for web applications.

Trying2Learn
Messages
375
Reaction score
57
TL;DR
IO in javascript: what is the question
Hello

A few weeks ago (March 7) I asked how to have Javascript read a data file of numbers into an array.

I now understand why this is such a challenge when running a code on a web page, locally.

I have since found a sufficient workaround. I hope I can share it in the hope that someone can tell me how I SHOULD have asked the question. (Yes, this is an odd question: I am actually asking FOR a question.)

Here is what I did.

After Matlab processed all the numbers, I had Matlab open a file named "Rotation.js"
Matlab wrote the lines into Rotation.js to create an open of a function... then it deposited the data array.
Then it wrote the lines to "close the function."
Then it closed the file.

Then I ran the *.html file that ran the thereJS code. The main.js called the function that Matlab created, and displayed the
results in 3D.

So... I know that is a silly thing to have done (I KNOW I could have coded in OpenGL, or written the javascript code to process
the data. But considering the complexity of the Runge-Kutta method on six differential equations, this was a good workaround.

What SHOULD I have asked to have gotten this result? Again, i am not asking for an answer or even a better way. I just want to know how I could have asked the question.

Here is the section from Matlab to show you what I did.fileID = fopen('get_RotationMatrix.js','w');
fprintf(fileID,'function get_RotationMatrix(){\n');
fprintf(fileID,'var rotation = [\n');
for i = 1:timeIntegrationSteps
fprintf(fileID,'\"%12.8f\",\n',omega1(i));
end
fprintf(fileID,'];\n');
fprintf(fileID,'return rotation;\n}\n');
fclose(fileID);
 
Technology news on Phys.org
Nice workaround. In essence, you used a server-side program to create the necessary javascript containing your data that could then be retrieved by the browser and then processed on your web page.

In the context of javascript, your question is without meaning. I suppose if you had asked about processing data in Matlab and then how to display it on a web page you might gotten better responses but we'll never know as programming is such a diverse platform of recipes to do the same thing in different ways.

The problem with your question is that:
1) you are using javascript that traditionally works in a sandbox on a browser where local files simply can't be read or written.
2) you need to do things outside the browser to get it to work

NodeJS is a framework/server where you can use javascript on both the server-side and browser-side. In this case, javascript running on NodeJS can access files to read and to write.

You might read your binary data, compute stuff and then write out a JSON file with the data. There are methods that write data in JSON format for you.

The browser side of your application could request a computation and then retrieve the JSON file to process and display.
 
  • Like
Likes   Reactions: Trying2Learn and FactChecker
Trying2Learn said:
What SHOULD I have asked to have gotten this result?
How about "I want to load a data file into an HTML page (so I can use it as input to the 3D renderer Three.js), how can I do this? I want all this to be local, not through a web server or anything."
 
  • Like
Likes   Reactions: Trying2Learn
jedishrfu said:
The browser side of your application could request a computation and then retrieve the JSON file to process and display.
That won't work with a local file, the XMLHttpRequest api won't work with file:// and although a script tag will happily parse a JSON file there is no way to access the data in the user sandbox.
 
Last edited:
My apologies, that’s not what I meant the json data is sent back to the browser via the response to the request.
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
10K
  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
18K